While Expressions/Loops

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 you’re never changing the actual underlying thing, just reusing the same name to point at something else.

Now, we’re going to cover the basics of both mutation and iteration. There is more to cover on both of these topics that we will get to in later lessons. Our goal now is to undertstand the bare minimum so that we can get into more fun stuff in Rust.

To start off, we want to understand why we’d want to mutate things.

Let’s remember the simple statement form of an if expression, which gives us the option to do something: