std-method

Move backward with signed offsets

offset takes a signed element count. Movement must respect the same allocation and distance constraints as ordinary in-bounds arithmetic. Derive the interior pointer from the array base, then move backward within that allocation. Expected stdout (not executed): 5 Common mistake: Treating offset(-1) on an array base as a valid preceding element.

Formal Syntax & Structure

rust syntaxrust
unsafe { p.offset(count: isize) }

Working Example

Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() {    let items = [4_i32, 5, 6];    let base = items.as_ptr();    // SAFETY: both offsets stay in the same live array.    let last = unsafe { base.add(2) };    let middle = unsafe { last.offset(-1) };    // SAFETY: middle addresses an initialized element; shared read only.    println!("{}", unsafe { *middle });}
Rust Reference