Redeclare immutable variable and change type - Shadowing
- When shadowing we can also change the type of the variable.
- We don't even need to make it mutable.
- e.g. we read from a file or from STDIN a string that we then convert to a number. We can use the same variable name.
examples/variables/change-type/src/main.rs
fn main() { let answer = "What is the answer"; println!("{answer}"); let answer = "42"; println!("{answer}"); let answer = 42; println!("{answer}"); println!("{}", answer + 1); }
What is the answer 42 42 43