keyword
Rust unsafe: mark operations with extra safety obligations
Unsafe Rust permits specific operations that the compiler cannot fully verify, such as dereferencing a raw pointer. The author must uphold each operation’s safety requirements. Ordinary type and borrow checks still apply. Expected output (not executed): 7 Why: The pointer refers to the still-live initialized integer and the read copies it. Common mistake: An unsafe block does not make invalid pointers safe or disable all compiler checks.
Formal Syntax & Structure
rust syntaxrust
unsafe { operation }Working Example
Example Snippetrust
fn main() { let value = 7_i32; let ptr = &raw const value; // SAFETY: ptr comes from a live, aligned, initialized i32; no mutation occurs. let copy = unsafe { *ptr }; println!("{copy}");}