Rust Raw Pointers: When References Aren't Enough
Raw pointers (*const T, *mut T) are Rust's C-style pointers, bypassing the borrow checker. They're used for FFI or building low-level abstractions. The footgun is assuming they're safe; they can be null or dangling, requiring unsafe to dereference.
Why it exists
Rust's borrow checker is powerful but conservative. It sometimes rejects valid code because it cannot prove its safety. Raw pointers provide an escape hatch for these situations, allowing programmers to implement patterns the compiler doesn't understand, especially when interfacing with other languages or performing low-level system optimizations.
The mental model
Think of raw pointers as a contract with the compiler. By creating one, you are saying, "I will manually uphold Rust's memory safety rules for this pointer." The compiler allows you to create them in safe code, but to dereference them and access the underlying data, you must wrap the operation in an unsafe block, explicitly acknowledging you are taking responsibility for safety.
How it works
Raw pointers exist as const T (immutable) and mut T (mutable). You can create them by casting references, like let ptr = &my_var as const i32;. Unlike Rust's safe references (&T), raw pointers are not bound by the borrow checker's rules. They can be null, they can point to invalid memory (be "dangling"), and you can have multiple mutable pointers to the same location. Creating them is always safe; using them (dereferencing with ) is not, and must happen inside an unsafe block.
When to use it
The two primary use cases are calling code in other languages (Foreign Function Interface, or FFI), especially C libraries that heavily use pointers, and building safe, high-level abstractions whose internal workings are too complex for the borrow checker to verify. For example, the implementation of Vec<T> uses raw pointers internally to manage its growable buffer, but it exposes a completely safe public API.
When not to use it
Avoid raw pointers in regular application code whenever possible. If your problem can be solved with safe Rust constructs like references, Box<T>, Rc<T>, or Arc<T>, you should always use them. Reaching for unsafe and raw pointers negates many of Rust's core safety guarantees and should be a last resort, not a convenience.
One canonical example
Creating both an immutable and a mutable pointer to the same data is forbidden with references but allowed with raw pointers. Consider let mut num = 5;. You can create let r1 = &num as const i32; and let r2 = &mut num as mut i32;. To use them, you enter an unsafe block: unsafe { println!("Value via r1: {}", r1); r2 = 10; }. Here, you are responsible for ensuring that reading from r1 while r2 could be writing does not cause a data race.
Interview question
Which statement accurately describes the safety implications and typical use cases of Rust's raw pointers?
- a.Raw pointers are primarily used for FFI and building low-level abstractions, and their dereferencing requires an unsafe block.Correct
- b.Raw pointers are a direct replacement for Box<T> when managing heap-allocated data that needs to be shared.
- c.They provide a safe way to bypass the borrow checker for performance-critical code without needing unsafe blocks.
- d.Creating raw pointers is always unsafe, but dereferencing them can be safe if done within a match statement.
Why? this is the answer
The card explicitly states that raw pointers are used for FFI and building low-level abstractions, and that while creating them is safe, dereferencing them requires an unsafe block. Option C is incorrect because raw pointers do not offer a safe way to bypass the borrow checker without `unsafe` blocks; they shift the responsibility for safety to the programmer.
Just read this? Test yourself on what you have been reading.
Read the original → doc.rust-lang.org
- #rust
- #memory management
- #pointers
- #unsafe
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on rust — each one lists the topics its interview covers.
See open roles