DEV Community

Cover image for πŸ¦€ Rust Master Class - Chapter 9: Smart Pointers
Oludayo Adeoye
Oludayo Adeoye

Posted on

πŸ¦€ Rust Master Class - Chapter 9: Smart Pointers

πŸ¦€ Rust Master Class - Chapter 9: Smart Pointers


Two people sharing custody of a cat. She can't be in two places at once, so they take turns. That's Rc and Arc β€” shared ownership, no drama.


In Rust, smart pointers are data structures that act like pointers but also have additional metadata and capabilities, such as automatic memory management [1-3]. The three most common smart pointers in the standard library are Box<T>, Rc<T>, and RefCell<T> [4-7].

1. Box<T>: Heap Allocation and Recursive Types

Box<T> is the simplest smart pointer. It allows you to store data on the heap rather than the stack [15.1, 223]. On the stack, the box contains a pointer to the heap-allocated data .

  • Key Characteristics:
    • Single Ownership: A Box has exactly one owner [15.1, 254].
    • Recursive Types: It is essential for defining types whose size cannot be known at compile time, such as a Cons list or a tree, where a type needs to hold another instance of itself [10-12].
  • When to Use:
    • When you have a type with an unknown size at compile time and you need to use it in a context that requires a fixed size [15.1, 22, 53].
    • When you want to transfer ownership of a large amount of data and ensure that the data is not copied [15.1, 223].
    • When you want to own a value that implements a specific trait rather than being a specific concrete type (trait objects) [15.1, 133].

2. Rc<T>: Multiple Ownership (Reference Counting)

Rc<T> (Reference Counted) enables multiple ownership of the same data [15.4, 258, 415]. It keeps track of the number of references to a value; when the count drops to zero, the data is cleaned up [13-15].

  • Key Characteristics:
    • Shared Access: It allows multiple parts of your program to read the same data on the heap [15.4, 258].
    • Immutability: By default, Rc<T> only allows immutable borrows [15.4, 260, 417].
    • Single-Threaded: It is not thread-safe; for multi-threaded scenarios, you must use Arc<T> [15.4, 113, 114].
  • When to Use:
    • When you need to share data on the heap but cannot determine at compile time which part of the program will finish using it last [15.4, 258].
    • In complex data structures like graphs or linked lists where multiple edges or nodes might point to the same location [16-19].

3. RefCell<T>: Interior Mutability

RefCell<T> implements the interior mutability pattern, allowing you to mutate data even when you have immutable references to it [15.5, 259, 417].

  • Key Characteristics:
    • Runtime Borrowing: Unlike standard references, RefCell enforces Rust’s borrowing rules at runtime rather than compile time [15.5, 259, 417].
    • Panic on Failure: If you violate borrowing rules (e.g., having two mutable borrows at once), the program will panic at runtime [15.5].
  • When to Use:
    • When you are certain your code follows borrowing rules, but the compiler's static borrow checker is too restrictive to recognize it [15.5].
    • Commonly used in combination with Rc (Rc<RefCell<T>>) to allow multiple owners to mutate the same shared data [16-23].

Summary Table

| Smart Pointer | Ownership | Mutability | Borrow Check |
| :


πŸ“– Download the full PDF: https://drive.google.com/file/d/1BOC2bl2zKMN9jljMCenPWgVXQC9fZ1Sh/view?usp=sharing

Part 9 of the Rust Master Class series β€” STEM EdTech | Automation Consulting | Rust Tutoring

RustLang #Programming #LearnToCode #STEM #EdTech


πŸ“š Practice Resources

GitHub Repository: https://github.com/PacktPublishing/Rust-Programming-Master-Class-from-Beginner-to-Expert

Try it yourself: https://play.rust-lang.org/

Run the code from this chapter in the Rust playground, then clone the repo to continue your Rust journey!


Part 9 of the Rust Master Class series β€” STEM EdTech | Automation Consulting | Rust Tutoring

RustLang #Programming #LearnToCode #STEM #EdTech

Top comments (0)