I want to insert or update a value in the map, and then get the number of keys.
use std::collections::HashMap;
fn main() {
let mut map = HashMap::new();
let count = map.entry("Tom").or_insert(0);
*count += 1;
let size = map.keys().len();
println!("{} men found", size);
}
The compiler error:
error[E0502]: cannot borrow `map` as immutable because it is also borrowed as mutable
--> src/main.rs:8:16
|
5 | let count = map.entry("Tom").or_insert(0);
| --- mutable borrow occurs here
...
8 | let size = map.keys().len();
| ^^^ immutable borrow occurs here
9 | println!("{} men found", size);
10 | }
| - mutable borrow ends here
Is there any way to work around this? Is the way I wrote it wrong?