expression

Validate an offset and length before reading

A boundary API must validate both arithmetic and range. Wasm bounds checks guard the linear memory extent, not the validity of an application object or Rust slice. This safe slice model checks a supplied range without raw pointers. Expected host stdout (not executed): valid: Some([98, 99]) overflow rejected: true Common mistake: Checking offset alone or assuming Wasm bounds checks make all Rust unsafe accesses valid.

Formal Syntax & Structure

rust syntaxrust
offset.checked_add(len); memory.get(offset..end)

Working Example

Example Snippetrust
fn region(memory: &[u8], offset: usize, len: usize) -> Option<&[u8]> {    memory.get(offset..offset.checked_add(len)?)}fn main() {    let memory = b"abcd";    println!("valid: {:?}", region(memory, 1, 2));    println!("overflow rejected: {}", region(memory, usize::MAX, 2).is_none());}
Rust Reference