Smart Pointers¶
Definition¶
- while references only borrow data, in many cases, smart pointers own the data they point to.
- Smart pointers implement the
DerefandDroptraits.
Some Important Smart Pointers in Rust¶
- String
- Vec
Most common smart pointers in the standard library¶
-
Box<T>for allocating values on the heap.Rc<T>, a reference counting type that enables multiple ownership.Ref<T>andRefMut<T>, accessed throughRefCell<T>, a type that enforces the borrowing rules at runtime instead of compile time -
With references and
Box<T>, the borrowing rules’ invariants are enforced at compile time. WithRefCell<T>, these invariants are enforced at runtime. With references, if you break these rules, you’ll get a compiler error. WithRefCell<T>, if you break these rules, your program will panic and exit. -
The
Box<T>type has a known size and points to data allocated on the heap. TheRc<T>type keeps track of the number of references to data on the heap so that data can have multiple owners. TheRefCell<T>type with its interior mutability gives us a type that we can use when we need an immutable type but need to change an inner value of that type; it also enforces the borrowing rules at runtime instead of at compile time.