Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding entries to a HashMap and getting references to them in a for loop

Tags:

rust

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.

like image 757
lc2817 Avatar asked Jan 23 '18 06:01

lc2817


1 Answers

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.

like image 114
nyro_0 Avatar answered Nov 15 '22 07:11

nyro_0