std-method

Transform an address while preserving provenance

map_addr is a convenient strict-provenance address transformation, available since Rust 1.84. Here the pointee is u8, so advancing one address unit selects the next byte. It is not a general replacement for bounds checks or element-wise arithmetic. Expected stdout (not executed): 91 Common mistake: Using byte increments to advance arbitrary pointee types without accounting for their size and alignment.

Formal Syntax & Structure

rust syntaxrust
p.map_addr(|address| new_address)

Working Example

Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() {    let bytes = [90_u8, 91];    let p = bytes.as_ptr();    let next = p.map_addr(|a| a.checked_add(1).unwrap());    // SAFETY: one byte forward stays in bytes, retaining p's provenance; read only.    println!("{}", unsafe { *next });}
Rust Reference