- 15 Smart Pointers
- 15-1 Using Box to Point to Data on the Heap
- 15-2 Treating Smart Pointers Like Regular References with the
DerefTrait- 15-2-1 Following the Pointer to the Value with the Dereference Operator
- 15-2-2 Using
Box<T>Like a Reference - 15-2-3 Defining Our Own Smart Pointer
- 15-2-4 Treating a Type Like a Reference by Implementing the
DerefTrait - 15-2-5 Implicit Deref Coercions with Functions and Methods
- 15-2-6 How Deref Coercion Interacts with Mutability
- 15-3 Running Code on Cleanup with the
DropTrait - 15-4
Rc<T>the Reference Counted Smart Pointer - 15-5
RefCell<T>and the Interior Mutability Pattern - 15-6 Reference Cycles Can Leak Memory
- 16 Fearless Concurrency
- 16-1 Using Threads to Run Code Simultaneously
- 16-2 Using Message Passing to Transfer Data Between Threads
- 16-3 Shared-State Concurrency
- 16-4 Extensible Concurrency with the
SyncandSendTraits
- 19 Advanced Features
- 19-1 Unsafe Rust
- 19-2 Advanced Traits
- 19-2-1 Specifying Placeholder Types in Trait Definitions with Associated Types
- 19-2-2 Default Generic Type Parameters and Operator Overloading
- 19-2-3 Fully Qualified Syntax for Disambiguation, Calling Methods with the Same Name
- 19-2-4 Using Supertraits to Require One Trait's Functionality Within Another Trait
- 19-2-5 Using the Newtype Pattern to Implement External Traits on External Types
- 19-3 Advanced Types
- 19-4 Advanced Functions and Closures
- 19-5 Macros
15 Smart Pointers
Reference counting smart pointer enables you to have multiple owners of data by keeping track of the number of owners and, when no owners remain, cleaning up the data.
References are pointers that only borrow data; in contrast, in many cases, smart pointers own the data they point to.
Smart pointers are usually implemented using structs. The characteristic that distinguishes a smart pointer from an ordinary struct is that smart pointers implement the Deref and Drop traits.
- The
Dereftrait allows an instance of the smart pointer struct to behave like a reference so you can write code that works with either references or smart pointers. - The
Droptrait allows you to customize the code that is run when an instance of the smart pointer goes out of scope.