Given a HashMap<String, String>
find the longest key.
This makes me sad:
let mut klen = 0;
for (k, _) in &map {
if k.len() > klen {
klen = k.len();
}
}
How would one express this using a more functional style of Rust?
Given a HashMap<String, String>
find the longest key.
This makes me sad:
let mut klen = 0;
for (k, _) in &map {
if k.len() > klen {
klen = k.len();
}
}
How would one express this using a more functional style of Rust?
(&map).map(|(key, _)| key.len()).max()
map.keys().map(String::len).max()
I hope we'll eventually be able to write map(_::len)
. Would make this a bit nicer to use.
Is this something that's being worked on?
Not to my knowledge, but we can already write _
for type inference in some places, e.g. my_iterator.collect::<Vec<_>>()
and this would be a neat expansion on that feature, IMO.
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.