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.
Fix bump: increment the key's count or insert 1 -- one borrow, via the entry API.
Hints
- The entry API does look-up-or-insert in ONE borrow:
map.entry(key). *map.entry(key.to_string()).or_insert(0) += 1;replaces the whole match.
Reveal solution
use std::collections::HashMap;
fn bump(map: &mut HashMap<String, i32>, key: &str) {
*map.entry(key.to_string()).or_insert(0) += 1;
}