Here's a Rust playground link, but:
fn main() {
// marking var as mutable
let mut counter = 0;
counter += 1;
counter += 3;
println!("counter: {}", counter);
// "shadowing" a variable: this is allocating mem for new var,
// and replacing the old one after
let myvar = 5;
let myvar = 1000;
println!("myvar: {}", myvar);
}
This gives the warning:
warning: unused variable: `myvar`
--> src/main.rs:19:9
|
19 | let myvar = 5;
| ^^^^^ help: if this is intentional, prefix it with an underscore: `_myvar`
|
= note: `#[warn(unused_variables)]` on by default
warning: 1 warning emitted
I don't quite understand. I use it later in the println macro. Why is it saying it is unused?
Perhaps I don't quite understand shadowing yet: I thought it was effectively allocating space for a new variable, writing the new value there, and then making the symbol myvar point to that memory.