Binding name vs memory location vs memory location value


#1

Concern stemming from video
NYLUG Presents - Steve Klabnik on Why Rust
time 48:06

fn main() {
let mut x = 5;
x = 6;
}
In the above, the binding ‘x’ - that is, the memory location known as x - can be changed. However, the ‘5’ cannot, so that writing “x = 6;” means that the location that x is referring to has changed. Did the memory location that held the 5 get freed when x was assigned the location where the compiler put the 6?

Here is my concern. Considering the above explanation, it would seem that if I changed the line “x = 6” to “x = i” and put “x = i” in a loop, 1 to a trillion, then the compiler would have to assign a trillion memory locations, which is not the right thing.

Is this a real concern? If not, why not? How is this to be thought of?

Thanks


#2

Even though it’s conceptually a rebinding, the compiler optimizes things,so it’s not going to end up literally doing that.


#3

Thanks


#4

I also find that distinction confusing for value types (for pointers it makes more sense). From this issue arielb explained it as:

The “binding of x” vs. “value at x” is more interesting in languages
with implicit pointers - in Rust, unless an explicit pointer dereference
is involved, the “value at x” is always an rvalue and changing it is
meaningless (in the same sense as 2 = 3;).


#5

The compiler ultimately decides where and if a value is stored in memory.