I am trying to add multiple elements to a HashMap
in a for
loop but can't seem to get it right:
use std::collections::HashMap;
fn set_if_needed_and_get(hmap: &mut HashMap<String, String>, st: String) -> &String {
hmap.entry(st.clone()).or_insert(st.clone())
}
fn main() {
let meeting_one_email = ["email1", "email2", "email1"];
let mut hmap: HashMap<String, String> = HashMap::new();
let mut attendees: std::vec::Vec<&String> = std::vec::Vec::new();
for m in meeting_one_email.iter() {
attendees.push(set_if_needed_and_get(&mut hmap, m.to_string()));
}
}
I get the error:
error[E0499]: cannot borrow `hmap` as mutable more than once at a time
--> src/main.rs:14:51
|
14 | attendees.push(set_if_needed_and_get(&mut hmap, m.to_string()));
| ^^^^ mutable borrow starts here in previous iteration of loop
15 | }
16 | }
| - mutable borrow ends here
I understand that I cannot borrow hmap
as mutable more than once, so how can I solve this while still using a for loop? Using a set and inserting in batches would work, but I want to use a for loop.
Your problem is not that you are trying to add elements to your
HashMap
in a loop, it's that you are modifying your hashmap and trying access yourhmap
in the scope of the loop.As you have a mutable borrow on
hmap
, you are not allowed to push its elements to yourattendees
vector in the loop. Adding a value to theHashMap
may require that the hashmap reallocate itself, which would invalidate any references to values inside it.One easy solution for your problem could be this:
In this code, you are accessing the hashmap after filling it up to also fill your
attendees
vector.