Search⌘ K

More Than Once!

Explore how to use mutable variables and while loops in Rust to build repetitive program logic. Understand incrementing counters within loops and practice coding exercises that print controlled output multiple times. This lesson helps you gain confidence in Rust's loop constructs and variable mutability for effective iteration.

We'll cover the following...

If you squint at the program in the previous lesson, our while loop isn’t much of a loop, as it still only runs the body once. Can we modify it so that it prints, “Inside the while loop,” four times, while only using one println! call? Yes, we can!

Challenge exercise: I’ll show you the answer in a second, but try to figure out the solution yourself. ...