I think an object that was moved from one binding to another simply means the object bits themselves stay put; just that program source refers to it with a different binding (identifier).
use std::fmt;
struct Person {
name: String,
age: u8,
}
impl Clone for Person {
fn clone(&self) -> Self {
Person {
name: self.name.clone(),
age: self.age,
}
}
}
impl fmt::Pointer for Person {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let ptr = self as *const Self;
fmt::Pointer::fmt(&ptr, f)
}
}
fn main() {
let p = Person {
name: "Krishna".to_string(),
age: 8,
};
println!("{:p}", p);
let a = p.clone();
println!("{:p}", a);
let q = p; // moved
println!("{:p}", q);
}
This prints
0x7ffee28b4178 // P
0x7ffee28b41f8 // A (P's clone)
0x7ffee28b4260 // Q (moved from P)
Why are the addresses of p
and q
different? It was compiled with rustc test.rs
.