Start with the idea
Start by reading each let as a promise about one binding. In let total = 10, the name total cannot be reassigned. The next line, let total = total + 5, does not change that first binding: the right side reads its value, then a new binding with the same name hides it. This is shadowing. It is useful when data passes through a clear transformation, such as trimming text and then parsing it, because the name can follow the meaning of the value even when its type changes.
Mutation is a different promise. let mut counter = 0 permits counter += 1 to update one existing binding. Choose mut when the same piece of state evolves over time, such as a counter in a loop; choose shadowing when a new stage of computation replaces the old view. Neither choice makes every value in the program mutable. Try removing mut and read the compiler error, then try replacing the update with a new let counter = counter + 1.
Keep learning, for free
The rest of this lesson is waiting for you.
Create a free account to read the complete explanation, see the infographic, explore the code, and try the exercises. All 742 lessons and algorithm steps are included.
No payment details. Your account opens the complete Academy.