Understanding unsafe Rust

Updated

October 31, 2026

Understanding unsafe Rust

unsafe Rust does not disable the borrow checker; it provides access to five superpowers: 1. Dereferencing raw pointers (*const T, *mut T). 2. Calling unsafe functions or foreign function interfaces (FFI). 3. Implementing unsafe traits. 4. Mutating mutable static state. 5. Accessing fields of union types.

fn main() {
    let mut num = 5;
    let r1 = &num as *const i32;

    unsafe {
        println!("Raw pointer points to: {}", *r1);
    }
}