Into_values alternative or workaround

I've found myself wanting to use an into_ variant of HashMap.values() only to find this issue:
https://github.com/rust-lang/rust/issues/55214

What would you recommend as a workaround?
I found the following "pattern match solution" when writing a mode function - working on the summary exercises for chapter 8 of the book:

But I wonder if this section (snipped):

fn mode(...) -> Option<i32> {
    ....
    match counts.values().max() {
        Some(&x) => Some(x),
        None => None,
    }
}

is sensible, it seems verbose to me (even for rust). I want to return Option and not the Option<&i32> max returns. It feels like I'm missing a helpful function. If not into_values, then what, if anything?

I also wonder if I'm missing some of the nuances of iterators and chaining .values() and .max(), perhaps?

Any help or advice would be appreciated, is there a better way? Thanks for reading :slight_smile:

counts.values().max().copied()

Option (what max returns), has an enormous number of small utility functions! There is almost one for any use-case

2 Likes

Ah ha! Thank you so much, I should have known to check what tools Option had to offer!

But I was distracted by the iterator methods :upside_down_face:

Now I recall the book's advice:

Becoming familiar with the methods on Option<T> will be extremely useful in your journey with Rust.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.