Sub topic 2
What makes a pointer smart
This chapter assumes you know Rust ownership, moves, and borrowing rules. It also assumes you know basic traits and generics, and the difference between the stack and the heap. Later chapters cover Box, Rc, and RefCell in detail. This one explains the idea they share.
A reference versus a smart pointer
A pointer is a value that holds a memory address. The most common pointer in Rust is a reference, written &T or &mut T. A reference only borrows. It points at data owned by someone else, has no behavior of its own, and cannot outlive the data it points to.
A smart pointer is a data structure that acts like a pointer and also carries extra metadata or capabilities. In most cases a smart pointer owns the data it points to. Dropping the pointer drops the data.
| Property | Reference (`&T`) | Smart pointer (for example `Box<T>`) |
|---|---|---|
| Owns the data | No, it borrows | Usually yes |
| Extra behavior | None | Allocation, counting, runtime checks |
| Cleanup | Nothing happens when it ends | Runs code when it is dropped |
| Defined as | Built-in language feature | Struct in the standard library or your own code |
You have already used smart pointers. String and Vec<T> own heap data and carry metadata such as length and capacity. They free their memory when they go out of scope.
The two traits behind the behavior
A smart pointer is usually a struct that implements two traits:
Dereflets you use*on the value and lets the compiler treat it like a reference. This is why code written for&Toften works with a smart pointer.Droplets you run code when the value goes out of scope, such as freeing memory or releasing a resource.
Without these traits, a struct that wraps a value is just a struct. With them, it behaves like a pointer with a lifecycle. Later chapters implement both traits yourself.
What the standard library gives you
Each of the main smart pointers changes one rule you know from ownership and borrowing.
| Type | What it changes |
|---|---|
| `Box<T>` | Puts a value on the heap. There is still exactly one owner. |
| `Rc<T>` | Allows several owners of one value, tracked by a reference count. |
| `RefCell<T>` | Moves the borrow rules from compile time to runtime, so you can mutate through a shared reference. |
Why the extra power has a cost
Each capability costs something. Box adds a heap allocation. Rc adds a counter that must be updated. RefCell adds a runtime check, and a violated rule becomes a panic instead of a compile error. The compiler still enforces safety. In the RefCell case it just cannot prove the rules ahead of time, so the program checks them as it runs.
Choose the simplest type that solves your problem. A plain reference or an owned value is often enough.
Exercise
Name two differences between a reference &T and a smart pointer such as Box<T>.
Show answer
A reference only borrows data that someone else owns, while a smart pointer usually owns the data. A smart pointer also implements Drop, so it runs cleanup code when it goes out of scope, and it implements Deref, so it can be used like a reference. A plain reference has neither behavior.
Exercise
Which smart pointer would you consider if you needed several owners of one read-only value in single-threaded code?
Show answer
Rc<T>. It keeps a reference count and frees the value when the last owner is dropped.