Add maximum and minimum and other functions

There should be a convenience way to get the maximum of an array or a set. Which would work like this.

assert_eq!([1,2,3].maximum(), 3);
assert_eq!([1,2,3].minimum(), 1);

There is already max on iterators for this purpose:

assert_eq!([1, 2, 3].into_iter().max().unwrap(), 3);
5 Likes

There's also this, experimentally: smallest in std::cmp - Rust

3 Likes

This isn't necessarily optimized for the collection type though, right? I would imagine some collections have a much more efficient way to find the max/min element that doesn't require iterating and comparing over all items.

Then they should override Iterator::min & Iterator::max to do the more efficient thing, like Range does.

4 Likes

(FWIW this thread is in the "language design" category but probably should be in "libs". There are no new language feature being discussed here at the moment.)