On this page
  1. unsafe
  2. Availability
  3. Syntax & Overview
  4. Notes & Pitfalls
  5. Example

unsafe

Prev: match | Up: builtins | Next: macro_rules
Rel: builtins, Ownership, Borrowing & Lifetimes
Ref: Rust Docs — unsafe
Tags: #public #prog #rust #builtins #tier/specialized


Availability

Built-in / Prelude (Language Keyword) — available with zero use imports.

Syntax & Overview

unsafe { /* raw ptr deref, extern FFI, unsafe fn */ }

Opts into 5 low-level operations (dereferencing raw pointers, calling unsafe fn/FFI, mutable statics, unsafe traits, union fields) while keeping the borrow checker active.

Notes & Pitfalls

Example

fn main() {
    let mut x = 10;
    let ptr: *mut i32 = &mut x;
    // SAFETY: `ptr` is non-null, aligned, and points to valid live stack memory `x`.
    unsafe {
        *ptr += 5;
    }
    assert_eq!(x, 15);
}

Linked from

Related notes