Hey there,
As I understand it, it is possible to have an immutable reference and a mutable one as long as the lifetime of the immutable one is gone by the time you declare the mutable.
In the code bellow the first method compiles while the second one doesn't, both with an immutable and a mutable reference, I thought the lifetime of the immutable reference would end with the loop but that doesn't seem to be the case, so what's really going on here ?
#![allow(dead_code)]
use std::collections::HashMap;
use std::hash::Hash;
use std::borrow::Borrow;
struct Test {
map: HashMap<String, String>
}
impl Test {
fn print_and_remove<Q>(&mut self, key: &Q)
where
String: Borrow<Q>,
Q: Eq + Hash + ?Sized,
{
match self.map.get(key) {
Some(v) => println!("{v}"),
None => {
println!("Not found");
return;
}
};
self.map.remove(key);
}
fn remove_by_value(&mut self, value: &String) {
let mut keys = Vec::new();
for (k, v) in self.map.iter() {
if v == value {
keys.push(k);
}
}
for key in keys {
self.map.remove(key);
}
}
}
The compiler message:
Compiling playground v0.0.1 (/playground)
error[E0502]: cannot borrow `self.map` as mutable because it is also borrowed as immutable
--> src/lib.rs:37:13
|
30 | for (k, v) in self.map.iter() {
| --------------- immutable borrow occurs here
...
36 | for key in keys {
| ---- immutable borrow later used here
37 | self.map.remove(key);
| ^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here
For more information about this error, try `rustc --explain E0502`.
error: could not compile `playground` due to previous error