3 ms·
The behavior is not the same. It's a new variable, entirely. So you'll have two integers on the stack, one of them is not accessible any more. You can see this
by steveklabnik 10y ago
The behavior is not the same. It's a new variable, entirely. So you'll have two integers on the stack, one of them is not accessible any more. You can see this difference if you add a scope around the second x, and then print the value of the original x after it goes out of scope.
Consider this program:
fn main() {
let x = 5;
let r1 = &x;
let x = x + 1;
let r2 = &x;
println!("r1: {:p}", r1);
println!("r2: {:p}", r2);
}
This will print something like
r1: 0x7fff955e3dfc
r2: 0x7fff955e3dec
Note that while you can't access the original x, it's still there, and references to it still work. Because the value of x was copied, not mutated.
Also, given compiler details like SSA, I'd argue that in a certain sense, mutation is similar to this, rather than this being similar to mutation...