Search⌘ K

Exercises

Explore exercises designed to practice mutable variables and while loops in Rust. Learn to replace recursion with iteration, generate nested loop outputs, print bordered patterns, and work with loops for various counting tasks, building strong control flow skills.

Exercise 1

Last time, we implemented a factorial function using recursion. As a reminder, a factorial multiplies all the numbers from 1 to the target, so fact(5) == 1 * 2 * 3 * 4 * 5. Write a fact function using iteration and a while loop instead of recursion.

Rust 1.40.0
#[allow(unused_variables, unused_mut)]
fn fact(x: i32) -> i32 {
// Write your code here!
0
}

Exercise 2

Write a program that uses nested while loops to produce this output:

X
XX
XXX
XXXX
XXXXX
XXXXXX
XXXXXXX
XX
...