Overview - Variable shadowing
What is it?
Variable shadowing in Rust means declaring a new variable with the same name as a previous one in the same scope. This new variable temporarily hides or 'shadows' the old one. It allows you to reuse names while changing the value or type safely. Shadowing is different from mutability because it creates a new variable instead of changing the old one.
Why it matters
Shadowing helps keep code clean and readable by reusing variable names without needing many unique names. Without shadowing, you would have to invent new names for every small change, making code confusing. It also allows transforming data step-by-step in a clear way. Without it, Rust code would be more verbose and harder to follow.
Where it fits
Before learning shadowing, you should understand basic variable declaration and mutability in Rust. After shadowing, you can learn about scopes, lifetimes, and ownership, which build on how variables behave. Shadowing is a foundation for writing idiomatic Rust code that is both safe and clear.