I have started Rust 2 days ago and I am pacing myself through the rust book. While going through the "Variables and Mutability" section, the following lines are written about variable shadowing and I am quoting the lines below:
" The other difference between
mut and shadowing is that because we’re effectively creating a new variable when we use the
let keyword again, we can change the type of the value but reuse the same name."
Going by the above text, I have few questions:
In a given scope, if we shadow a variable, a new variable is created. What happens to the
variable that was shadowed? Is it still accessible or it gets destructed automatically as soon
as it gets shadowed?
What is meant by "reuse the same name"? Is the name of a variable indicates a memory
address which the compiler can reuse for shadowed variables? Does shadowed variables
share same memory address ?
What happens to the data that a shadowed variable holds?
Why shadowing in same scope was designed in Rust? What specific benefits we derive
when we use shadowing in same scope?