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.
We'll cover the following...
We'll cover the following...
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.
Exercise 2
Write a program that uses nested while loops to produce this output:
X
XX
XXX
XXXX
XXXXX
XXXXXX
XXXXXXX
XX ...