What Are Raw Pointers in Rust and When to Use Them?

Raw pointers are unsafe, unchecked memory addresses used for FFI and advanced data structures, requiring explicit unsafe blocks to dereference.

Raw pointers in Rust are unsafe, non-owning pointers (*const T and *mut T) that bypass the borrow checker to allow manual memory management. You use them for FFI, implementing unsafe data structures, or when you need to create reference cycles that smart pointers cannot handle.

fn main() {
    let x = 5;
    let r = &x as *const i32;
    let m = &mut x as *mut i32;

    unsafe {
        println!("Value: {}", *r);
        *m = 10;
    }
}

Use unsafe blocks to dereference raw pointers, as the compiler cannot guarantee their validity or safety.