Borrow checker Advanced

26  Moving out

You can't move a value out from behind a reference -- that would leave a hole. The fix is to swap something valid into its place and hand back the old value. std gives you take for exactly this.

Step 2 / 2

Fix Slot::take: take the item out of &mut self, leaving None behind.

Hints
  • Option::take does exactly this: self.item.take().
26_move_out.rs
Reset to starterCompiled in a sandbox; hidden checks must pass.
Reveal solution
struct Slot { item: Option<String> }

impl Slot {
    fn take(&mut self) -> Option<String> {
        self.item.take()
    }
}