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

Fix take_at: return the String at index i, leaving an empty String behind. You can't move out of a Vec by indexing.

Hints
  • std::mem::take(&mut v[i]) swaps in String::default() (empty) and returns the old value.
26_move_out.rs
Reset to starterCompiled in a sandbox; hidden checks must pass.
Reveal solution
fn take_at(v: &mut Vec<String>, i: usize) -> String {
    std::mem::take(&mut v[i])
}