Skip to main content

Unsafe Rust

unsafe doesn't mean memory-unsafe. It means you are responsible for maintaining the invariants the compiler can no longer verify.

The five unsafe superpowers:

  1. Dereference raw pointers
  2. Call unsafe functions or methods
  3. Access or modify mutable static variables
  4. Implement unsafe traits
  5. Access fields of unions

Everything else — including the borrow checker — still applies inside unsafe blocks.

Raw Pointers*const T and *mut T — aliasing, alignment, validity
Unsafe TraitsSend, Sync, and custom unsafe contracts
FFIextern "C", #[no_mangle], C interop patterns
Safety InvariantsHow to document and enforce unsafe contracts