I'm trying to get the keys of a BTreeMap
with u32
keys.
When I use the .iter().keys()
method, it returns a reference to the keys: &u32
.
I understand the logic behind getting a reference to the key because it doesn't consume the data structure, but since u32
implements the Copy
trait, I figured it is possible to get u32
directly.
The only way I found of doing this is by mapping over all the keys and dereferencing them:
let map = BTreeMap::from([
(0, "foo"),
(1, "bar"),
(2, "baz")
])
let keys: Vec<u32> = map.iter().map(|(k, _)| *k).collect();
Is there a better, faster or more concise way of doing this?