我刚刚开始学习铁 rust ,发现了一种看起来很奇怪的HashMap's entry() method岁行为.在下面的示例中,该方法采用可变引用并返回Entry enum.我甚至没有捕获和持久化返回值.但borrow 判断器似乎认为在下一次迭代开始时,对"s"的可变引用仍在作用域中.
let mut window: HashMap<&String, i32> = HashMap::new();
let mut s = "help_me".to_string();
loop {
let p = &mut s; // ERROR
window.entry(p);
}
这将显示代码段死机,并显示错误:
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
有人能给我解释一下这种行为吗?