In the below example (extracted from https://doc.rust-lang.org/stable/core/pin/index.html#what-is-pinning) rust panics even in release mode.
Why would rust moves bits physically even though trackera is invalid after trackerb owns the value. Isn't this costly operation ?
#[derive(Default)]
struct AddrTracker(Option<usize>);
impl AddrTracker {
fn check_for_move(&mut self) {
let current_addr = self as *mut Self as usize;
match self.0 {
None => self.0 = Some(current_addr),
Some(prev_addr) => assert_eq!(prev_addr, current_addr),
}
}
}
pub fn main() {
let mut trackera = AddrTracker::default();
trackera.check_for_move();
let mut trackerb = trackera;
trackerb.check_for_move();
}
Types that pin data to a location in memory.