The Copy trait - what does it actually copy?

Thank you all so much!

As I now understand, &str and &i8 behave the same (maybe as all &T), so when the Rust Book says:

let x = 5;
let y = x;

We can probably guess what this is doing: “bind the value 5 to x; then make a copy of the value in x and bind it to y.” We now have two variables, x and y, and both equal 5. This is indeed what is happening, because integers are simple values with a known, fixed size, and these two 5 values are pushed onto the stack.

The value being Copy is the integer 5.


But if we were to instead create a reference, i.e. &T:

let x = &5;
let y = x;

The value being Copy the reference to a hardcoded 5 somewhere off in read-only memory, i.e. not the stack or heap.

1 Like