How can I store multiple elements in a Rust HashMa

2020-04-10 13:25发布

I have a HashMap<u32, Sender>. Sender is a open connection object and the key is a user id. Each user can connect from multiple devices. I need to store all possible open connections for the same user id. After this I can iterate and send messages to all open connections for same user.

The above HashMap only stores each user id and connection once. I need to get one key with multiple values. How can I make the value into a list or an array, so I can see which connections exist and send to them all?

I am not talking about different value types, like enums. I am talking about the same type values but more than one. Maybe HashMap is not designed for this?

Alternative ideas are also welcomed.

1条回答
▲ chillily
2楼-- · 2020-04-10 13:59

To do this with a HashMap you should use a Vec as the values, so that each key can point to multiple Senders. The type then would be HashMap<u32, Vec<Sender>>.

Using this structure, just using insert() can get clunky when you need to mutate the values like this, but instead you can use the Entry API for retrieving and updating records in one go. For example:

let mut hash_map: HashMap<u32, Vec<Sender>> = HashMap::new();

hash_map.entry(3)
    // If there's no entry for key 3, create a new Vec and return a mutable ref to it
    .or_insert_with(Vec::new)
    // and insert the item onto the Vec
    .push(sender); 

You could also use the multimap crate, which does something similar under the hood, but adds a layer of abstraction. You might find it easier to work with:

let mut multi_map = MultiMap::new();

multi_map.insert(3, sender_1);
multi_map.insert(3, sender_2);

The method multi_map.get(key) will the first value with that key, while multi_map.get_vec(key) will retrieve all of them.

查看更多
登录 后发表回答