quest

Sign in to start training

quest keeps your progress, streaks, and scores tied to your account — so you can pick up on any device.

Synced everywhere. Progress follows you across laptop and phone.
Streaks & stats. See your daily run and every score over time.
Your profile. A name and avatar for the work you do here.

We only use Google to sign you in and sync progress. No posting, no contacts — ever.

Applied Intermediate

16  Smart pointers

Box gives a value a fixed-size home on the heap -- which is what makes recursive types possible. Rc adds shared ownership: several owners of one allocation, freed when the last one drops.

Step 1 / 2

Sum the recursive List in sum. Box is what lets the type nest.

Hints
  • Recurse on the tail: a node's total is its value plus the rest's total.
  • match list { List::Node(v, rest) => v + sum(rest), List::End => 0 } -- &Box<List> coerces to &List.
16_smart_pointers.rs
Reset to starterCompiled in a sandbox; hidden checks must pass.
Reveal solution
enum List {
    Node(i32, Box<List>),
    End,
}

fn sum(list: &List) -> i32 {
    match list {
        List::Node(v, rest) => v + sum(rest),
        List::End => 0,
    }
}