6

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.

3
  • Possibly related: How do I mutate a structure I am looping over?
    – ljedrz
    Jan 23, 2018 at 7:42
  • 1
    If you were able to do this, you could end up with a bunch of invalid references in attendees array. HashMap can reallocate its storage.
    – red75prime
    Jan 23, 2018 at 8:05
  • 2
    Short answer: you can't (store references and then invalidate them by mutating the container).
    – red75prime
    Jan 23, 2018 at 8:26

1 Answer 1

8

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 your hmap in the scope of the loop.

As you have a mutable borrow on hmap, you are not allowed to push its elements to your attendees vector in the loop. Adding a value to the HashMap 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:

fn main() {
    let meeting_one_email = ["email1", "email2", "email1"];

    let mut hmap: HashMap<String, String> = HashMap::new();

    for m in meeting_one_email.iter() {
        set_if_needed_and_get(&mut hmap, m.to_string());
    }
    let attendees: Vec<&String> = hmap.keys().collect();
}

In this code, you are accessing the hashmap after filling it up to also fill your attendees vector.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.