I have got a hashmap, which I want to iterate over, mutate some elements, filter out some elements and take remaining keys (or map it to something else). I face compiler error cannot borrow data mutably in a
& reference
, which I understand why but do not know what to do about it. Could you please suggest what is Rust approach to work with mutable HashMaps using fluent syntax?
Here is the playground
use std::collections::HashMap;
fn main() {
let mut map: HashMap<String, HashMap<String, String>> = HashMap::new();
process_mutable_map(&mut map);
}
fn process_mutable_map(map: &mut HashMap<String, HashMap<String, String>>) {
for (key, value) in map.iter_mut() {
let a: &mut HashMap<String, String> = value; // here I can take element value as mutable ref
}
let res: Vec<String> = map
.iter_mut()
.filter(|e| {
// but here I can not take element value as mutable ref, because it is a immutable ref to a tuple !!?
let element: &mut HashMap<String, String> = e.1;
// mutate element here, skipped for simplicity
// assume nothing is taken for simplicity of the example
false
})
.map(|e| e.0.clone())
.collect();
}
if I can not use filter
, I have got the option to use for
loop and build vector of remaining keys via some sort of extendable buffer. Using fluent syntax is not possible in this case with HashMap?