diff --git a/src/lib.rs b/src/lib.rs index ee4f3ab78..36ddef6cc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4084,6 +4084,23 @@ pub trait Itertools: Iterator { /// assert_eq!(a.iter().minmax(), MinMax(&1, &1)); /// ``` /// + /// ``` + /// use itertools::Itertools; + /// use itertools::MinMaxResult::{MinMax, NoElements, OneElement}; + /// + /// let a: [(i32, char); 0] = []; + /// assert_eq!(a.iter().minmax(), NoElements); + /// + /// let a = [(1, 'a')]; + /// assert_eq!(a.iter().minmax(), OneElement(&(1, 'a'))); + /// + /// let a = [(0, 'a'), (1, 'b')]; + /// assert_eq!(a.iter().minmax(), MinMax(&(0, 'a'), &(1, 'b'))); + /// + /// let a = [(1, 'a'), (1, 'b'), (1, 'c')]; + /// assert_eq!(a.iter().minmax(), MinMax(&(1, 'a'), &(1, 'c'))); + /// ``` + /// /// The elements can be floats but no particular result is guaranteed /// if an element is NaN. fn minmax(self) -> MinMaxResult @@ -4105,6 +4122,27 @@ pub trait Itertools: Iterator { /// /// The keys can be floats but no particular result is guaranteed /// if a key is NaN. + /// + /// # Examples + /// + /// ``` + /// use itertools::Itertools; + /// use itertools::MinMaxResult::{MinMax, NoElements, OneElement}; + /// + /// let cmp_key = |x: &&(i32, char)| x.0; + /// + /// let a: [(i32, char); 0] = []; + /// assert_eq!(a.iter().minmax_by_key(cmp_key), NoElements); + /// + /// let a = [(1, 'a')]; + /// assert_eq!(a.iter().minmax_by_key(cmp_key), OneElement(&(1, 'a'))); + /// + /// let a = [(0, 'a'), (1, 'b')]; + /// assert_eq!(a.iter().minmax_by_key(cmp_key), MinMax(&(0, 'a'), &(1, 'b'))); + /// + /// let a = [(1, 'a'), (1, 'b'), (1, 'c')]; + /// assert_eq!(a.iter().minmax_by_key(cmp_key), MinMax(&(1, 'a'), &(1, 'c'))); + /// ``` fn minmax_by_key(self, key: F) -> MinMaxResult where Self: Sized, @@ -4122,6 +4160,27 @@ pub trait Itertools: Iterator { /// For the minimum, the first minimal element is returned. For the maximum, /// the last maximal element wins. This matches the behavior of the standard /// [`Iterator::min`] and [`Iterator::max`] methods. + /// + /// # Examples + /// + /// ``` + /// use itertools::Itertools; + /// use itertools::MinMaxResult::{MinMax, NoElements, OneElement}; + /// + /// let first_item_cmp = |x: &&(i32, char), y: &&(i32, char)| x.0.cmp(&y.0); + /// + /// let a: [(i32, char); 0] = []; + /// assert_eq!(a.iter().minmax_by(first_item_cmp), NoElements); + /// + /// let a = [(1, 'a')]; + /// assert_eq!(a.iter().minmax_by(first_item_cmp), OneElement(&(1, 'a'))); + /// + /// let a = [(0, 'a'), (1, 'b')]; + /// assert_eq!(a.iter().minmax_by(first_item_cmp), MinMax(&(0, 'a'), &(1, 'b'))); + /// + /// let a = [(1, 'a'), (1, 'b'), (1, 'c')]; + /// assert_eq!(a.iter().minmax_by(first_item_cmp), MinMax(&(1, 'a'), &(1, 'c'))); + /// ``` fn minmax_by(self, mut compare: F) -> MinMaxResult where Self: Sized,