Rust's Variable Shadowing: Re-binding, Not Mutating
Shadowing lets you declare a new variable with the same name, making the old one inaccessible. It's used to transform a value, like changing its type, without making it mutable. The footgun is confusing shadowing (let x = ...) with reassignment (x = ...).
Why it exists
Shadowing allows for convenient, readable transformations of data. Instead of creating slightly different variable names like input_str and input_num, you can reuse the same name for the value as it evolves. This avoids making variables mutable when they don't need to be, adhering to Rust's preference for immutability.
The mental model
Think of shadowing as placing a new label over an old one on a file folder. The old label is hidden, but not destroyed. In a new, inner scope (like inside curly braces), you can place a temporary label. Once you leave that scope, the temporary label is removed, revealing the one from the outer scope again.
How it works
Using the let keyword on a name that's already in scope creates a completely new variable. This new variable can even have a different type than the one it shadows. The original variable becomes inaccessible for the rest of the current scope. Unlike mutation (using let mut and reassigning), which changes the value in the same memory location, shadowing can allocate new memory for the new variable.
When to use it
Use shadowing when you want to perform a few transformations on a value but treat it as immutable between steps. It's perfect for changing a variable's type, such as parsing a string from user input into a number. For example: let guess = "42"; let guess: u32 = guess.parse().expect("Not a number!");.
When not to use it
Avoid shadowing if you need to modify a value many times, especially in a loop. A mutable variable (let mut) is the correct tool for that job, as it's more efficient and clearly signals that the value is expected to change repeatedly. Overuse in complex functions can make it hard to track which version of a variable you're using.
One canonical example
A common pattern is processing a value through several steps. First, declare a string with extra whitespace: let x = " 5 ";. Then, shadow it with a trimmed version: let x = x.trim();. Finally, shadow it again with a parsed integer type: let x: i32 = x.parse().unwrap();. You now have a clean integer x with the value 5, having used the same name through three different types and values without requiring mutability.
Interview question
What is the fundamental difference between variable shadowing and variable mutation in Rust?
- a.Shadowing uses the let mut keyword to rebind a variable, while mutation uses let for simple value updates.
- b.Shadowing is preferred for single-step transformations, while mutation is used for multiple, iterative value changes.
- c.Shadowing allows a variable to change its type, whereas mutation only permits changes to its value without altering its type.
- d.Shadowing declares a new variable that makes the previous one inaccessible, while mutation modifies the value of an existing variable in place.Correct
Why? this is the answer
Shadowing creates a completely new variable, potentially allocating new memory, making the original inaccessible. Mutation, however, changes the value of an existing variable in its current memory location. Option B describes when to use each, not the fundamental difference in their operation.
Just read this? Test yourself on what you have been reading.
Read the original → doc.rust-lang.org
- #rust
- #variables
- #shadowing
- #immutability
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on rust — each one lists the topics its interview covers.
See open roles