Back to questions
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
fn main() {
let x = 5;
let x = x * 2;
{
let x = x + 1;
println!("{x}");
}
println!("{x}");
}11 10
- Expecting
11 11: theletinside the block makes a separatexthat hides the outer one only until the block's}. - Expecting a redeclaration error: a second
let xis legal in Rust and shadows the firstxinstead of clashing with it.
- How would the output change with
let mut xoutside andx += 1;inside the block? - Can a shadowing
letgivexa different type, and why?