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 2 / 2

In make_duo, give both players the SAME team allocation -- clone the Rc, not the String.

Hints
  • Rc::clone(&team) bumps the reference count -- it never copies the String.
  • Give one player a clone and let the other take team itself.
16_smart_pointers.rs
Reset to starterCompiled in a sandbox; hidden checks must pass.
Reveal solution
use std::rc::Rc;

struct Player { name: &'static str, team: Rc<String> }

fn make_duo(team: Rc<String>) -> (Player, Player) {
    let a = Player { name: "ash", team: Rc::clone(&team) };
    let b = Player { name: "brook", team };
    (a, b)
}