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.
Read the code
let total = 10; // immutable
let total = total + 5; // shadowed
let mut counter = 0; // mutable
counter += 1;Shadowing makes a new binding; mut changes an existing one.
Practice starter
let value = 5;
value = 6;
Work through it
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. Compare which version better communicates your intent to the next reader.
A useful test is to put a transformation at the same scope and ask whether the earlier form should still be available. Shadowing hides that earlier binding under the reused name; it does not mutate memory by magic. In a nested block, a shadowed name can stop hiding the outer one when the block ends. This is why naming and scope matter as much as the presence of mut.
Think of a binding as a name attached to a value, not a box that must change. let score = 3; creates an immutable binding by default. A later assignment to that same binding needs let mut score = 3; at the declaration. This rule helps you see which parts of a program can change while it runs. It does not mean all underlying data in Rust is forever fixed; it describes what this binding permits.
Shadowing is different from assignment. A second let score = ...; starts a new binding with the same name. The old value is no longer reachable by that name in the new scope, and the new value can even have a different type. An assignment such as score = ...; keeps the same binding and must respect its type. Trace each let in the example with a small table: line, name, value, type, and whether later assignment is permitted. That table is more useful than memorizing that one syntax is 'mutable' and the other is not.
Try making a counter with mut and increment it twice. Predict the final printed value. Then rewrite the same calculation using a new shadowing let for each step. Finally attempt to assign text to a numeric mutable binding and read the type error. If a name appears in a nested block, mark where that block starts and ends; a shadowing binding exists only within its scope. These experiments prepare you for ownership: Rust tracks not just what a value is, but which binding may use it at each point.
Try it
Create an immutable value, shadow it with a transformed value, and update one mutable counter. Predict which names can be reassigned before compiling; then remove mut once and explain the diagnostic.
What success looks like: The final program compiles without an unnecessary-mut warning. You can point to the new binding created by shadowing and to the single existing binding updated through mut.
Check your understanding
Why is the reassignment rejected?
Reveal the answer
- 5 and 6 are different types
- value is immutable without mut
- Numbers cannot be reassigned
Add mut, or shadow with a second let if a new binding is what you meant.
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Variables (courseId "basics", lessonId "variables") Link: https://crabrix.com/learn/basics/variables/ Goal: mut permits mutation; shadowing hides the old binding behind a new one. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=basics/variables) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.