0xc0
July 13, 2021, 7:19pm
1
I would like to write a function to convert from a map whose key is a pair to a map whose key is the first from the pair and value is a map whose key is the second from the pair, with the types as follows:
fn tuple_key_to_map_of_map(tupled: BTreeMap<(String, String), i32>) -> BTreeMap<String, BTreeMap<String, i32>> {
// ?
}
What is an idiomatic way to code this function?
Here's the way to do it.
fn tuple_key_to_map_of_map(tupled: BTreeMap<(String, String), i32>) -> BTreeMap<String, BTreeMap<String, i32>> {
let mut new = BTreeMap::new();
for ((first, second), value) in tupled {
new.entry(first).or_insert_with(BTreeMap::new).insert(second, value);
}
new
}
2 Likes
system
Closed
October 11, 2021, 8:08pm
3
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.