The following is written in the Rust documentation:
let s1 = String::from("hello"); let s2 = s1;When we assign s1 to s2, the String data is copied, meaning we copy the pointer, the length, and the capacity that are on the stack. We do not copy the data on the heap that the pointer refers to.
When I run the following code,
let s1 = String::from("aa");
let p2 = &s1;
println!("p2:{:p}", p2);
let s2 = s1;
let p3 = &s2;
println!("p3:{:p}", p3);
Output:
p2:0x7ffc1bd2e730
p3:0x7ffc1bd2e7a0
Why is the address pointed by p2 and the address pointed by p3 not the same?
Stringstruct values on the stack, not the string itself. That output pretty much proves that the documentation is correct. - E_net4 the voter