According to the Rust book, "when a binding goes out of scope, the resource that they’re bound to are freed". Does that also apply to shadowing?
Example:
fn foo() {
let v = vec![1, 2, 3];
// ... Some stuff
let v = vec![4, 5, 6]; // Is the above vector freed here?
// ... More stuff
} // Or here?
No, it is not freed immediately. Let's make the code tell us itself:
The output is:
For me, this has come in handy in cases where you transform the variable into some sort of reference, but don't care about the original. For example: