Sub topic 1
Ownership and Borrowing: The Rules Smart Pointers Bend
Smart pointers make sense only against the rules they adjust. This chapter reviews those rules and marks the three places where Box, Rc, and RefCell change them.
Prerequisites
This chapter assumes you already know:
- Rust ownership, moves, and the basic borrowing rules.
- References and lifetimes at a basic level.
- Structs, enums, and pattern matching.
- Traits and generics at a basic level.
- The difference between stack and heap memory.
The three rules
Rust's compiler enforces these rules at compile time:
- Every value has exactly one owner.
- When the owner goes out of scope, the value is dropped.
- You may have either one mutable reference or any number of shared references to a value at a time, but not both. References must always be valid.
Rule 1 gives you predictable cleanup with no garbage collector. Rule 3 prevents data races and use-after-free bugs.
The rules in code
The program below moves a value, then borrows it.
fn main() {
let a = String::from("hello");
let b = a; // ownership moves to b; a can no longer be used
let r1 = &b;
let r2 = &b; // many shared references are fine
println!("{r1} {r2}");
let mut c = String::from("world");
let m = &mut c; // one mutable reference
m.push('!');
println!("{m}");
}Using a after the move, or taking &mut c while r1-style shared references to c are still in use, would fail to compile.
Where the rules feel tight
The rules are strict, so some valid designs are hard to write:
- A value whose size the compiler cannot determine, such as a type that contains itself.
- A value that needs several owners, such as a node shared by two parents in a graph.
- A value you must mutate even though you only hold a shared reference to it.
Smart pointers address these cases. Each one bends a specific rule, and the rest of the rules still apply.
Which rule each pointer bends
| Pointer | What changes | When the rule is checked |
|---|---|---|
| `Box<T>` | Owns a value on the heap. Ownership stays single. | Compile time |
| `Rc<T>` | Allows several owners of one value, read-only. | Compile time for borrows, runtime for the count |
| `RefCell<T>` | Allows mutation through a shared reference. | Runtime |
The key point is that the rules themselves do not go away. RefCell<T> still enforces "one mutable borrow or many shared borrows". It checks that rule while the program runs and panics on a violation, instead of rejecting the program at compile time.
Why the tradeoff exists
The compiler analyzes code conservatively. It rejects some programs that are actually safe, because it cannot prove they are safe. Runtime checks let you write those programs, at the cost of a small overhead and the risk of a panic if you get the logic wrong. Use the compile-time rules by default, and reach for a smart pointer only when you can name which rule is getting in your way.
Exercise
Which of the three pointers, Box, Rc, or RefCell, moves borrow checking from compile time to runtime, and which rule does it still enforce?
Show answer
RefCell<T>. It still enforces that you can have either one mutable borrow or any number of shared borrows, but it checks this while the program runs and panics if the rule is broken.