I need to get get the key associated with the largest value in a BTreeMap. (Doing this the other way around is simple.)
My attempt so far is:
let mut opt_pair: Option<(&Foo, u32)> = None;
for (key, value) in my_btreemap { // my_btreemap is known to be non-empty
match opt_pair {
Some(pair) => {
if value > pair.1 {
opt_pair = Some((key, value));
}
},
None => {
opt_pair = Some((key, value));
}
}
}
opt_pair.unwrap().0
Is there an idiomatic way of doing this, in a more functional style?