Aliasing and provenance limitations
Quick answer
Aliasing rules constrain competing memory accesses. Pointer provenance describes the allocation-related authority associated with a pointer, not merely its numeric address.
Preserve the origin of access
Shared references permit shared access subject to interior-mutability rules; mutable references impose exclusivity requirements. Raw pointers do not erase those requirements. Strict-provenance methods such as addr and with_addr separate address manipulation from provenance. Exact aliasing rules are not fully settled; Stacked Borrows and Tree Borrows are research models, not a finished language specification.
fn main() { let mut a = [1, 2, 3, 4]; let (left, right) = a.split_at_mut(2); left[0] = 9; right[0] = 8; println!("{a:?}");}Expected stdout (not executed):
[9, 2, 8, 4]The standard API establishes that the two regions do not overlap.
fn main() { let x = 5u32; let p = &x as *const u32; let q = p.with_addr(p.addr()); // SAFETY: unchanged address and provenance; x is alive and only read. println!("{}", unsafe { *q });}Expected stdout (not executed):
5with_addr retains p's provenance; changing the address would still require a valid access.
Common mistake
Do not infer dereference permission from two equal numeric addresses.
Try it yourself
Explain why split_at_mut is preferable to manufacturing two mutable references manually.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Aliasing and provenance limitations — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
Two mutable views must cover disjoint halves of a slice. Which safe API establishes this?
Aliasing and provenance limitations — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust fn main() { let mut a = [1, 2, 3, 4]; let (left, right) = a.split_at_mut(2); left[0] = 9; right[0] = 8; println!("{a:?}"); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Aliasing and provenance limitations — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust fn main() { let x = 5u32; let p = &x as *const u32; let q = p.with_addr(p.addr()); // SAFETY: unchanged address and provenance; x is alive and only read. println!("{}", unsafe { *q }); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.