Is there a simple way to fill a 2 dimensional HashMap
? In C++ I could do something along the lines of:
std::unordered_map<int,std::unordered_map<int,int>> 2dmap;
//...
2dmap[0][0] = 0;
2dmap[0][1] = 1;
//...
In my Rust project I am trying to fill a similar map:
let mut map: HashMap<i32, HashMap<i32, i32>> = HashMap::new();
//...fill the hash map here.
The only way that I can think to do this would be to build each sub map then move them into the super map, something like this:
let mut sub1: HashMap<i32, i32> = HashMap::new();
sub1.insert(0, 0);
sub1.insert(1, 1);
let mut map: HashMap<i32, HashMap<i32, i32>>::new();
map.insert(0, sub1);
Is there a more concise way to do this?
The above code is a simplification of my actual use case which uses an enum as the index of the HashMap
:
enum Example {
Variant1,
Variant2,
//...
}
None of the variants hold a value. I am using this syntax for the lookup from my HashMap
:
let value = map[Example::Variant1][Example::Variant2];