expression

Limit the scope of an unsafe block

Use an unsafe block only for the operation that requires it. Ordinary type checks and borrow restrictions still apply. The surrounding code establishes the facts needed for the read; the block does not make an invalid pointer valid. Expected stdout (not executed): 42 Common mistake: Wrapping an entire module in unsafe instead of identifying each obligation.

Formal Syntax & Structure

rust syntaxrust
unsafe { *pointer }

Working Example

Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() {    let value = 42_i32;    let pointer: *const i32 = &value;    // SAFETY: pointer comes from a live, initialized, aligned i32;    // the shared value is not mutated during the read.    let copy = unsafe { *pointer };    println!("{copy}");}
Rust Reference