While Expressions/Loops
Explore the basics of mutation and iteration in Rust by learning how to use mutable variables and while loops. Understand how while loops differ from if expressions, how to control loop execution, and how to avoid infinite loops. This lesson prepares you for more advanced Rust programming concepts.
We'll cover the following...
Previously, we saw recursion. That allowed us to do something more than once. However, in Rust, recursion isn’t the preferred way to make that happen. Instead, Rust prefers something called iteration. Iteration is a more direct way to say, “Do this more than once,” than recursion.
Iteration all but requires the usage of mutation. So far, all of the variables we’ve looked at in Rust have been immutable. Once created, you can’t change them. With shadowing and function calls, the exact value for a given name at a specific point in your code may appear to be different. But ...