Consider the following code snippet:
fn main() {
let mut v1 = vec![1, 2, 3];
println!("The address of vector v1 is {:p}", &v1);
let v2 = v1;
println!("The address of vector v2 is {:p}", &v2);
v1 = v2;
println!("The address of vector v1 is {:p}", &v1);
}
and the output
The address of vector v1 is 0x7fff253de570
The address of vector v2 is 0x7fff253de5e0
The address of vector v1 is 0x7fff253de570
Why does the value of v1 and v2 not the same?
- First, Doesn't
&v2really mean the address of vectorvec![1,2,3]as declared in line #2? - If the value of
v1is copied tov2then, should not the vector have a copy trait? - If the value of
v1is moved to a new memory location which is identified byv2, why is it at all required, why doesn't thev2simply point to the memory location ofv1, because the ownership is already transferred tov2, andv1is pretty much useless until I assign it back (in short, if it is a memory copy, why does the ownership transfer require amemcpy?) - When
v1is assigned tov2again, how did I get the same address location?