Skip to main content
Journey Uncommon Logo
JourneyUncommon
hardRustunsafe and the aliasing modelSingle-choice MCQ

Under the aliasing model Rust assumes, the optimizer is allowed to treat `&mut T` as 'no other access aliases this for its scope'. In the snippet, why does writing through `p` after creating `r` from the same `&mut` cause undefined behavior, even though no read happens between the two writes?

let mut x = 0i32; let p = &mut x; let r = &mut *p; // reborrow *p = 1; // use parent while child r still live *r = 2; // r used here println!("{}", *r);