3 ms·
There is effectively an implicit scope created by the let binding. In rust you can't reassign a variable without it being mut. The two `x`s are separate variabl
by stormbrew 6y ago
There is effectively an implicit scope created by the let binding. In rust you can't reassign a variable without it being mut. The two `x`s are separate variables named x and are not at any point mutated. Reassignment would look like this:
let x = 1;
x = x + 1; // <- error[E0384]: cannot assign twice to immutable variable `x`
let mut x = 1;
x = x + 1; // ok!
This obviously matters very little for an integer, but it is relevant to more complex types.
You can actually see the scopes, and the progress of variable liveness, if you run the compiler out to the MIR intermediate language:
fn main() -> () {
let mut _0: (); // return place in scope 0 at src/main.rs:1:11: 1:11
let _1: i32; // in scope 0 at src/main.rs:2:9: 2:10
scope 1 {
debug x => _1; // in scope 1 at src/main.rs:2:9: 2:10
let _2: i32; // in scope 1 at src/main.rs:3:9: 3:10
scope 2 {
debug x => _2; // in scope 2 at src/main.rs:3:9: 3:10
}
}
bb0: {
StorageLive(_1); // scope 0 at src/main.rs:2:9: 2:10
_1 = const 1_i32; // scope 0 at src/main.rs:2:13: 2:14
StorageLive(_2); // scope 1 at src/main.rs:3:9: 3:10
_2 = const 2_i32; // scope 1 at src/main.rs:3:13: 3:18
_0 = const (); // scope 0 at src/main.rs:1:11: 4:2
StorageDead(_2); // scope 1 at src/main.rs:4:1: 4:2
StorageDead(_1); // scope 0 at src/main.rs:4:1: 4:2
return; // scope 0 at src/main.rs:4:2: 4:2
}
}
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=8af2f63e25b8bc8bfdb43c1908cf7da4 https://play.rust-lang.org/?version=stable&mode=debug&editio...
- mcsalgado 6y agoOh interesting, I see now! Thanks for clearing this up.