Day 22: Smart Pointers
HardBoxRcRefCell

Day 22: Smart Pointers

Smart pointers are data structures that act like pointers but have additional metadata and capabilities.

Box<T>: Heap Allocation

Box<T> stores data on the heap instead of the stack:

let b = Box::new(5);  // 5 is stored on the heap
println!("{}", b);    // Deref coercion: prints 5

When to Use Box

  • When you have a type whose size can't be known at compile time
  • When you have a large amount of data to transfer ownership without copying
  • When you want to own a value that implements a trait (trait objects)

Recursive Types

Without Box, recursive types would have infinite size:

enum List {
    Cons(i32, Box<List>),
    Nil,
}

This pattern is exactly how linked lists work! The Box gives us a known size (pointer size) at compile time.

Other Smart Pointers

TypeUse Case
Box<T>Heap allocation, known size
Rc<T>Multiple ownership (single-threaded)
Arc<T>Multiple ownership (thread-safe)
RefCell<T>Interior mutability

Rc<T>: Reference Counting

use std::rc::Rc;

let a = Rc::new(5);
let b = Rc::clone(&a);  // Both point to same data
println!("count: {}", Rc::strong_count(&a));  // 2

The Task

Implement sum_list that recursively sums all values in a cons list.

Requirements

  • Cons(1, Box::new(Cons(2, Box::new(Cons(3, Box::new(Nil))))))6
  • Handle both Cons and Nil cases

Hints

fn sum_list(list: &List) -> i32 {
    match list {
        Cons(value, next) => value + sum_list(next),
        Nil => 0,
    }
}
Language: Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Output
Run to see the result here.
    Day 22: Smart Pointers · RUST Challenge | learn.sol