Remove value of vector in HashMap


How can I, concisely (one line) and without borrowing error, remove a value from Vec in HashMap ? Code example:

use std::collections::HashMap;

fn main() {
    let mut my_hash_of_vec: HashMap<usize, Vec<usize>> = HashMap::new();
    my_hash_of_vec.insert(0, vec![3, 1, 0, 2]);
    // Remove "1" value from vector at my_hash_of_vec 0 position

In my tests, i have cannot borrow *xxxxxas mutable as it is behind a & reference...

Thanks !

You probably want the get_mut method of the hashmap.


If you really want a one line solution :

bag.get_mut(&0).map(|vec| vec.swap_remove(1));

Oh you right ! I forgot to use get_mut ! Thanks for your one line solution :slight_smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.