What is Unsafe Rust?
All the code we've written so far has been Safe Rust, where the compiler guarantees memory safety. Unsafe Rust is a "superpower" that allows you to bypass some of those restrictions.
It exists because the compiler is sometimes too conservative—it might reject code that is actually safe just because it can't prove it.
The Unsafe Superpowers
Inside an
unsafe block, you can do five things you can't do in safe Rust:- Dereference a Raw Pointer.
- Call an unsafe function or method.
- Access or modify a mutable static variable.
- Implement an unsafe trait.
- Access fields of unions.
Raw Pointers (*const T and *mut T)
Raw pointers are like C pointers. Unlike references, they:
- Are allowed to ignore the borrowing rules (many mutable pointers to same data).
- Are not guaranteed to point to valid memory.
- Can be null.
- Don't have automatic cleanup.
rust
Calling Unsafe Functions
An unsafe function has the
unsafe keyword before its definition. You can only call it from within an unsafe block.rust
FFI: Foreign Function Interface
Wait! A common use for
unsafe is talking to code written in other languages (like C).rust
[!CAUTION] Usingunsafedoesn't turn off the borrow checker or other safety checks. It just gives you access to these five specific powers. Use it as a last resort!
Summary
| Feature | Description |
|---|---|
| unsafe | Keyword used to start an unsafe block or function |
| Raw Pointer | *const T / *mut T (No safety guarantees) |
| FFI | Interacting with C or other languages |
| Static Mut | Globally mutable state (DANGEROUS!) |
| Unsafe Trait | A trait that requires manual safety proof |
| Safety | You (the programmer) must guarantee memory safety |
| Best For | Low-level drivers, performance optimizations, FFI |
| Panic | Unsafe code can still panic! |
| Logic | Wrap unsafe code in safe abstractions (like Vec or String) |
| Modern | Most Rust apps have 0% unsafe code! |
| Memory | Unsafe is where you do manual memory management |