Memory mapping concepts
Quick answer
Memory mapping associates a region of virtual address space with pages supplied by the operating system. File-backed mappings can access file data through memory, but have important lifetime and modification constraints.
Separate logical ranges from OS mappings
Mapped pages may be loaded on demand, and mapping is not a guarantee of residency or speed. A file may be modified or truncated by another process, invalidating assumptions behind a Rust slice; file-backed memmap2 constructors therefore require unsafe contracts. The examples below only model bounded range access and owned bytes; they do not map actual files. For ordinary small inputs a bounded read into an owned buffer is often simpler.
fn region(data: &[u8], offset: usize, len: usize) -> Option<&[u8]> { data.get(offset..offset.checked_add(len)?)}fn main() { println!("{:?} {:?}", region(b"abcd", 1, 2), region(b"abcd", usize::MAX, 2)); }Expected stdout (not executed):
Some([98, 99]) NoneThe checked addition avoids overflow before the slice range is formed.
fn main() { let mut source = vec![1u8, 2, 3]; let snapshot = source.clone(); source[0] = 9; println!("{snapshot:?}");}Expected stdout (not executed):
[1, 2, 3]This copy is independent of later changes to the source buffer; a real mapped view is not necessarily independent.
Common mistake
Do not treat read-only mapping permissions as proof that no other process can change the backing file.
Try it yourself
Describe the guarantees you would need before exposing a file-backed mapping as a safe slice.
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
Memory mapping concepts — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A read-only mapping's backing file can be truncated by another process. Which assumption is unsafe?
Memory mapping concepts — 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 region(data: &[u8], offset: usize, len: usize) -> Option<&[u8]> { data.get(offset..offset.checked_add(len)?) } fn main() { println!("{:?} {:?}", region(b"abcd", 1, 2), region(b"abcd", usize::MAX, 2)); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Memory mapping concepts — 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 mut source = vec![1u8, 2, 3]; let snapshot = source.clone(); source[0] = 9; println!("{snapshot:?}"); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.