🦀 Rust Intermediate

What is unsafe Rust and what does it allow?

Answer

Unsafe Rust is a subset of Rust inside unsafe { } blocks that allows five additional capabilities not permitted in safe Rust: 1) Dereference raw pointers (*const T, *mut T). 2) Call unsafe functions or methods. 3) Access or modify mutable static variables. 4) Implement unsafe traits. 5) Access fields of union types. Unsafe does NOT disable the borrow checker — it merely allows these additional operations. The programmer must manually uphold safety invariants within unsafe blocks. The goal is to contain unsafety: you write a small unsafe abstraction and expose a safe public API. For example, Vec uses unsafe internally but is safe to use. Never write unsafe code unless you fully understand the invariants required.