Borrow checker Advanced

27  Double borrows

Two classics that look fine but borrow the same collection twice. The fixes are idioms you'll reuse constantly: the HashMap entry API, and collect-then-extend instead of mutating while iterating.

Step 2 / 2

Fix echo_evens: collect the doubled evens first, then extend after the loop.

Hints
  • Build the additions in a separate Vec first -- then nothing borrows v when you push.
  • let doubled: Vec<i32> = v.iter().filter(..).map(..).collect(); then v.extend(doubled);.
27_double_borrow.rs
Reset to starterCompiled in a sandbox; hidden checks must pass.
Reveal solution
fn echo_evens(v: &mut Vec<i32>) {
    let doubled: Vec<i32> = v.iter().filter(|&&x| x % 2 == 0).map(|&x| x * 2).collect();
    v.extend(doubled);
}