I've just started learning Rust and have come across a seemingly bizarre behaviour of HashMap's entry() method. In the below example, the method takes a mutable reference and returns the Entry enum. I am not even capturing and persisting the returned value. But the borrow checker seems to think a mutable reference to "s" is still in scope when the next iteration begins.
let mut window: HashMap<&String, i32> = HashMap::new();
let mut s = "help_me".to_string();
loop {
let p = &mut s; // ERROR
window.entry(p);
}
This shows snippet panics with error:
Line 27, Char 26: cannot borrow `s` as mutable more than once at a time (solution.rs)
|
27 | window.entry(&mut s);
| ^^^^^^ `s` was mutably borrowed here in the previous iteration of the loop
Can someone please explain this behaviour to me?