Search⌘ K

Failed Attempt: Shadowing

Explore how Rust handles variable shadowing within while loops and understand the scope rules that cause infinite loops. Learn why altering a shadowed variable inside the loop body does not affect the loop condition and how to use mutability correctly to control loop execution.

We'll cover the following...

Let’s try to use the tools we already have in our toolbox to make a while loop that exits. We’ll create a variable, keep_going. It will start off as true, and then we’ll make the variable equal to false inside the body of the while using name shadowing. As a reminder, name shadowing lets us create a new variable with the same name as the one before, e.g.: ...