When one owner isn't enough
Ownership as you learned it in lesson 4 is a tree: each value has exactly one owner, and borrows are temporary. Most data fits that shape, which is why most Rust code needs nothing in this lesson. But some data doesn't. A graph node may be pointed to by many others; a cache may be shared by several parts of a program; a counter may be updated by eight threads at once. For these, the standard library provides smart pointers: types that act like pointers but add rules of their own, such as counting their owners or checking borrows at run time.
The same tools are the foundation of Rust's approach to concurrency. In most languages, sharing data between threads is where the worst bugs live: two threads write the same value without coordination (a data race), and the result depends on timing, so the bug appears once a week on one customer's machine. Rust turns data races into compile errors, using the ownership rules you already know plus two marker traits, Send and Sync. The Rust community calls this fearless concurrency. It doesn't remove every concurrency bug (deadlocks and logic races are still possible), but it removes the one that is hardest to find.
At the bottom of every safe abstraction in this lesson there is a little unsafe code: a few lines where the compiler takes the programmer's word for something it can't check. The last section explains what that means and why it is not the hole in the bucket it might sound like.
Box: a value on the heap
Definition— Box
Box<T> owns a value of type T stored on the heap. The box itself is one pointer; when the box is dropped, the heap value is dropped and its memory freed. Boxes are used for recursive types, for trait objects (Box<dyn Trait>, lesson 6), and occasionally to move a large value without copying it.
// A recursive type needs indirection: Box gives each child a fixed-size pointer.
enum Expr {
Num(f64),
Add(Box<Expr>, Box<Expr>),
Mul(Box<Expr>, Box<Expr>),
Neg(Box<Expr>),
}
use Expr::{Add, Mul, Neg, Num};
fn eval(e: &Expr) -> f64 {
match e {
Num(x) => *x,
Add(a, b) => eval(a) + eval(b),
Mul(a, b) => eval(a) * eval(b),
Neg(a) => -eval(a),
}
}
fn show(e: &Expr) -> String {
match e {
Num(x) => x.to_string(),
Add(a, b) => format!("({} + {})", show(a), show(b)),
Mul(a, b) => format!("{} * {}", show(a), show(b)),
Neg(a) => format!("-{}", show(a)),
}
}
fn main() {
// (2 + 3) * -4
let e = Mul(
Box::new(Add(Box::new(Num(2.0)), Box::new(Num(3.0)))),
Box::new(Neg(Box::new(Num(4.0)))),
);
println!("{} = {}", show(&e), eval(&e));
let on_heap: Box<[u8; 4]> = Box::new([1, 2, 3, 4]);
println!(
"{} bytes behind a pointer of {} bytes",
on_heap.len(),
size_of::<Box<[u8; 4]>>()
);
}(2 + 3) * -4 = -20
4 bytes behind a pointer of 8 bytesAn Expr can contain other Exprs. Without the boxes, the compiler would have to make an Expr big enough to hold two Exprs, each big enough to hold two more, forever; it reports a recursive type "has infinite size" and suggests adding indirection. With boxes, each child is a pointer of known size. This shape (an enum of boxed sub-trees, evaluated by a recursive match) is how compilers, interpreters, query planners and JSON libraries written in Rust represent their trees. (Since Rust 1.80, size_of is available without importing it.)
Rc and RefCell: shared ownership, interior mutability
Definition— Rc and RefCell
Rc<T> (reference-counted) allows several owners of one heap value in a single thread. Rc::clone(&rc) makes another owner by incrementing a count, without copying the value; the value is dropped when the last owner goes. An Rc gives only shared (&T) access. RefCell<T> provides interior mutability: it lets you mutate its contents through a shared reference, by checking the borrowing rules at run time instead of compile time. borrow() gives a shared borrow, borrow_mut() an exclusive one, and breaking the rules panics.
use std::cell::RefCell;
use std::rc::Rc;
// Every member shares one shopping list: shared ownership (Rc) of a value
// that can be changed through a shared reference (RefCell).
type SharedList = Rc<RefCell<Vec<String>>>;
struct Member {
name: String,
list: SharedList,
}
impl Member {
fn new(name: &str, list: &SharedList) -> Member {
Member {
name: name.to_string(),
list: Rc::clone(list),
}
}
fn add(&self, item: &str) {
let entry = format!("{item} ({})", self.name);
self.list.borrow_mut().push(entry);
}
}
fn main() {
let list: SharedList = Rc::new(RefCell::new(Vec::new()));
println!("owners: {}", Rc::strong_count(&list));
let amina = Member::new("Amina", &list);
let tomas = Member::new("Tomás", &list);
println!("owners: {}", Rc::strong_count(&list));
amina.add("rice");
tomas.add("lemons");
amina.add("coriander");
println!("{:#?}", list.borrow());
// The borrow rules still apply, but they are checked at run time.
let reading = list.borrow();
match list.try_borrow_mut() {
Ok(_) => println!("got a mutable borrow"),
Err(e) => println!("refused while someone is reading: {e}"),
}
drop(reading);
list.borrow_mut().clear();
println!("cleared; {} items", list.borrow().len());
drop(tomas);
println!("owners after Tomás leaves: {}", Rc::strong_count(&list));
drop(amina);
println!("owners now: {}", Rc::strong_count(&list));
}owners: 1
owners: 3
[
"rice (Amina)",
"lemons (Tomás)",
"coriander (Amina)",
]
refused while someone is reading: RefCell already borrowed
cleared; 0 items
owners after Tomás leaves: 2
owners now: 1Each Member owns a clone of the Rc, so the list lives as long as any member does. add takes &self, yet changes the list: borrow_mut() hands out a temporary exclusive borrow, after checking at run time that no other borrow is active. While reading holds a shared borrow, try_borrow_mut reports an error; the plain borrow_mut would have panicked with "RefCell already borrowed". So the rule "many readers or one writer" still holds; it has just moved from compile time to run time, and the cost is a counter check per borrow plus the risk of a panic if you get it wrong.
Two cautions from experience. First, reach for these last: Rc<RefCell<T>> everywhere is often a sign of a design ported from a garbage-collected language; owning data in one place and passing references or indices usually leads to simpler code. Second, reference counting cannot free cycles: if A holds an Rc to B and B holds one to A, neither count reaches zero and both leak. Back-pointers (child to parent, observer to subject) should use Weak, a non-owning pointer obtained with Rc::downgrade, which must be upgraded (and may fail) before use. For simple Copy values, Cell gives interior mutability with no borrow tracking at all, by only ever copying values in and out.
Threads, scoped threads and channels
A thread is an independent line of execution that the operating system can run on another CPU core. std::thread::spawn starts one with a closure and returns a handle; join waits for it to finish and returns the closure's result.
use std::sync::mpsc;
use std::thread;
fn is_prime(n: u64) -> bool {
n >= 2
&& (2..)
.take_while(|d| d * d <= n)
.all(|d| !n.is_multiple_of(d))
}
fn main() {
// 1. spawn + join: the closure must own what it uses (`move`).
let label = String::from("background count");
let handle = thread::spawn(move || {
let count = (1..=50_000).filter(|&n| is_prime(n)).count();
format!("{label}: {count} primes up to 50,000")
});
println!("{}", handle.join().unwrap());
// 2. Scoped threads may borrow local data, because the scope
// guarantees they finish before the data goes away.
let readings: Vec<u64> = (1..=1_000).collect();
let (left, right) = readings.split_at(readings.len() / 2);
let (a, b) = thread::scope(|s| {
let h1 = s.spawn(|| left.iter().sum::<u64>());
let h2 = s.spawn(|| right.iter().sum::<u64>());
(h1.join().unwrap(), h2.join().unwrap())
});
println!("halves {a} + {b} = {}", a + b);
// 3. Channels: several producers, one consumer.
let (tx, rx) = mpsc::channel();
for worker in 0..3 {
let tx = tx.clone();
thread::spawn(move || {
for job in 0..2 {
let msg = format!("worker {worker} finished job {job}");
tx.send(msg).unwrap();
}
});
}
drop(tx); // the receiver's loop ends when every sender is gone
let mut messages: Vec<String> = rx.iter().collect();
messages.sort(); // arrival order varies from run to run
println!("{} messages; first: {:?}", messages.len(), messages[0]);
}background count: 5133 primes up to 50,000
halves 125250 + 375250 = 500500
6 messages; first: "worker 0 finished job 0"Three patterns, in increasing order of how often you will use them in real code:
thread::spawnwithmove. The new thread might outlive the function that started it, so it may not borrow local variables: its closure must own everything it uses (the bound isF: FnOnce() -> T + Send + 'static).movetransferslabelinto the thread, andjoinbrings the result back. If the thread panics,joinreturns anErrinstead of crashing the caller.thread::scope(stable since Rust 1.63). Threads spawned inside a scope are guaranteed to finish before the scope ends, so they may borrow local data: here two threads sum the two halves of one vector, with no copying and noArc. This is the easiest way to parallelise work over data you already have.- Channels.
mpsc::channel()returns a sender and a receiver ("multiple producer, single consumer"). Each worker gets its own clone of the sender;sendmoves a value into the channel, transferring ownership to whichever thread receives it. Once every sender has been dropped, iterating over the receiver ends. Message passing avoids shared state altogether, which is why many concurrent designs start here.
Notice that the program sorts the messages before printing them: the order in which threads run is decided by the operating system and changes from run to run. Never write tests that depend on it.
Async/await, in outline
Threads are a fine way to do a handful of things at once. A web server holding fifty thousand mostly idle network connections is another matter: fifty thousand operating-system threads would waste memory and time switching between them. For this, Rust has async/await.
An async fn does not run when called; it returns a future, a value representing work that can make progress, pause while waiting (for a network packet, a timer, a database reply), and resume later. Inside an async function, .await on another future means "pause me here until that is ready, and let something else run meanwhile". The compiler turns each async function into a state machine, so a paused task costs only the memory for its state, often a few hundred bytes.
async fn fetch_both(client: &Client) -> Result<(Page, Page), Error> {
let (a, b) = tokio::join!(client.get("/news"), client.get("/weather"));
Ok((a?, b?))
}Unusually, Rust's standard library defines futures but provides no runtime to drive them. You choose one as a dependency. Tokio is by far the most widely used, providing a multi-threaded scheduler, asynchronous networking, timers and synchronisation; the axum web framework and the tonic gRPC library are built on it. Embedded systems use runtimes such as Embassy. The trade-offs are real: async code adds concepts (pinning, cancellation when a future is dropped, Send bounds on tasks that move between threads) and a function-colouring split between async and ordinary code. Use it for I/O-heavy concurrency, such as servers, crawlers and proxies; for CPU-heavy work, threads and libraries such as rayon, which parallelises iterator chains, are simpler and faster.
Unsafe Rust and FFI
The compiler's checks are conservative: some correct programs can't be proven correct by them. And some code must talk to things the compiler knows nothing about: the operating system, hardware registers, libraries written in C. For these cases Rust has the unsafe keyword. Inside an unsafe block you gain exactly five extra abilities: dereferencing raw pointers, calling unsafe functions (including foreign ones), accessing mutable statics, implementing unsafe traits, and accessing fields of unions. Everything else, including the borrow checker, still applies.
// Declarations of functions from the C standard library, which every Rust
// program on Linux already links against. Since the 2024 edition the block
// itself must be marked `unsafe`: the compiler cannot check these signatures.
unsafe extern "C" {
fn abs(x: i32) -> i32;
fn strlen(s: *const std::ffi::c_char) -> usize;
}
/// A safe wrapper: callers can't misuse it, so it needs no `unsafe` itself.
fn c_abs(x: i32) -> i32 {
// SAFETY: `abs` has no preconditions for this input range.
unsafe { abs(x) }
}
fn main() {
println!("abs(-42) from C: {}", c_abs(-42));
let text = c"Kia ora"; // a C string literal (Rust 1.77+), ending in a zero byte
// SAFETY: `text` is a valid, zero-terminated string that outlives the call.
let n = unsafe { strlen(text.as_ptr()) };
println!("strlen says {n}");
// Raw pointers can be created in safe code; only dereferencing is unsafe.
let mut values = [10, 20, 30];
let p: *mut i32 = values.as_mut_ptr();
// SAFETY: index 2 is inside the array, and no other reference is in use.
unsafe {
*p.add(2) += 1;
}
println!("{values:?}");
}abs(-42) from C: 42
strlen says 7
[10, 20, 31]This program calls two functions from the C standard library through the foreign function interface (FFI). The unsafe extern "C" block declares their signatures; the compiler cannot check that they match the real C functions, which is why the 2024 edition requires the block itself to be marked unsafe. c"Kia ora" is a C string literal (Rust 1.77 and later), a zero-terminated string of the kind C expects. The last part shows that creating a raw pointer is safe, but reading or writing through it requires unsafe, because nothing guarantees the pointer is valid.
unsafe does not switch the checks off; it means "the programmer promises an invariant the compiler cannot verify". Breaking that promise causes undefined behaviour, the same class of bugs Rust otherwise prevents. So professionals contain it:
- Keep unsafe blocks small and wrap them in safe abstractions whose public API cannot be misused, like
c_absabove.Vec,Rc,MutexandStringare all built exactly this way. - Write a
// SAFETY:comment on every unsafe block explaining why the promise holds; Clippy can require it. - Test unsafe code with Miri, an interpreter that detects many kinds of undefined behaviour in tests (
cargo +nightly miri test). - For FFI, generate bindings with tools rather than writing them by hand: bindgen produces Rust declarations from C headers, cbindgen does the reverse, and cxx builds safe bridges to C++.
The result is that in a typical application, unsafe code is a tiny fraction of the total, concentrated in well-reviewed libraries, and a memory-safety bug can only originate in those few audited places.
Common misconceptions
Misconception
"Rust prevents all concurrency bugs." It prevents data races in safe code. Deadlocks (two threads each waiting for a lock the other holds), starvation, and logic races (checking a value and acting on it as two separate locked steps) are still possible. Keep lock scopes short and take locks in a consistent order.
Misconception
"Rc<RefCell<T>> is how you write normal Rust." It is a tool for genuinely shared, mutable, single-threaded data, such as some graph and GUI structures. Most programs need it rarely or never; owning data in one place and passing references is simpler and checked at compile time.
Misconception
"Async is faster than threads." Async lets one thread juggle many waiting tasks cheaply. It does not make computation faster, and for CPU-bound work it can be slower than plain threads. Choose by workload: many waits, async; heavy computation, threads or rayon.
Misconception
"unsafe turns off the borrow checker." It doesn't. References inside unsafe blocks are checked as usual. unsafe grants five specific extra abilities, chiefly dereferencing raw pointers and calling functions whose requirements the compiler can't check.
Exercises
1. Smart pointers, concurrency and unsafe
Warm-upComputationalWhat does this print? Write the two numbers separated by a space.
use std::rc::Rc;
fn main() {
let a = Rc::new(String::from("x"));
let b = Rc::clone(&a);
{
let c = Rc::clone(&b);
println!("{}", Rc::strong_count(&c));
}
println!("{}", Rc::strong_count(&a));
}
2. Smart pointers, concurrency and unsafe
CoreComputationalWhat happens when this runs?
use std::cell::RefCell;
fn main() {
let x = RefCell::new(1);
let _a = x.borrow_mut();
let _b = x.borrow_mut();
}
3. Smart pointers, concurrency and unsafe
CoreComputationalFour threads each run for _ in 0..250 { *total.lock().unwrap() += 1; } on the same Arc<Mutex<i32>> starting at 0, and main joins them all before printing the total. What is printed?
4. Smart pointers, concurrency and unsafe
CoreHumanEight threads must all add to one running total. Which of these will compile and work? Choose all that apply.
5. Smart pointers, concurrency and unsafe
StretchHumanWhich of these can you do inside an unsafe block that you cannot do outside one? Choose all that apply.
6. Smart pointers, concurrency and unsafe
CoreHumanWhich job is the best fit for async Rust with a runtime such as Tokio, rather than plain threads?
Further reading and resources
- The Rust Programming Language, chapter 15: Smart Pointers · Steve Klabnik, Carol Nichols, Chris Krycho and the Rust community
- The Rust Programming Language, chapter 16: Fearless Concurrency · Steve Klabnik, Carol Nichols, Chris Krycho and the Rust community
- The Rust Programming Language, chapter 17: Async and Await · Steve Klabnik, Carol Nichols, Chris Krycho and the Rust community
- The Rust Programming Language, section 20.1: Unsafe Rust · Steve Klabnik, Carol Nichols, Chris Krycho and the Rust community
- TokioThe most widely used async runtime, with a thorough tutorial.
- The RustonomiconThe official guide to writing unsafe Rust correctly.
- Rust Atomics and Locks · Mara Bos · 2023A book, free to read online, on how concurrency primitives work, by a member of the Rust library team.