Bind a name
let level = 3; evaluates the right-hand expression and gives its result a name. An immutable binding is a useful default for a quantity that should keep the same value. Declare let mut when later statements need to update that particular binding.
Follow a name into a block
At the opening brace the program enters a new scope. The inner let level = 8; creates a second binding. A read of level starts in the current scope, so the first print sees 8. Leaving the block removes the inner binding and the outer 3 is visible again.
Change an existing binding
An assignment starts with the name, as in level = 8;. The interpreter finds the nearest binding with that name. Assignment requires a mutable binding and a compatible value kind. If there is an inner declaration, updating it only changes the inner value.
Accumulate a result
For a mutable integer counter, count += 1 adds one to the current value. The same updates work for subtraction, multiplication, division and remainder through -=, *=, /= and %=. Start a sum at zero and a product at one so the initial value preserves the first term.
Try the program
Expected output
8 3
Practice
Make both print calls show 8 by updating the outer level inside the block.
Hint
Use mut for the outer declaration and remove let from the inner statement.
Worked solution
There is now one mutable binding. The assignment reaches it and the new value is still visible after the block.
Find
let level = 3;Replace with
let mut level = 3;Find
let level = 8;Replace with
level = 8;Output after the change
8 8