Skip to content

Commit 7e4e17d

Browse files
Takashiidobephimuemue
authored andcommitted
examples for minmax_by and minmax_by_key
1 parent 033e0ae commit 7e4e17d

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/lib.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4105,6 +4105,26 @@ pub trait Itertools: Iterator {
41054105
///
41064106
/// The keys can be floats but no particular result is guaranteed
41074107
/// if a key is NaN.
4108+
///
4109+
/// # Examples
4110+
/// ```
4111+
/// use itertools::Itertools;
4112+
/// use itertools::MinMaxResult::{MinMax, NoElements, OneElement};
4113+
///
4114+
/// let abs_key = |x: &&i32| x.abs();
4115+
///
4116+
/// let a: [i32; 0] = [];
4117+
/// assert_eq!(a.iter().minmax_by_key(abs_key), NoElements);
4118+
///
4119+
/// let a = [1];
4120+
/// assert_eq!(a.iter().minmax_by_key(abs_key), OneElement(&1));
4121+
///
4122+
/// let a = [-2, -1, 0, 1, 2];
4123+
/// assert_eq!(a.iter().minmax_by_key(abs_key), MinMax(&0, &2));
4124+
///
4125+
/// let a = [1, 1, 1, 1];
4126+
/// assert_eq!(a.iter().minmax_by_key(abs_key), MinMax(&1, &1));
4127+
/// ```
41084128
fn minmax_by_key<K, F>(self, key: F) -> MinMaxResult<Self::Item>
41094129
where
41104130
Self: Sized,
@@ -4122,6 +4142,27 @@ pub trait Itertools: Iterator {
41224142
/// For the minimum, the first minimal element is returned. For the maximum,
41234143
/// the last maximal element wins. This matches the behavior of the standard
41244144
/// [`Iterator::min`] and [`Iterator::max`] methods.
4145+
///
4146+
/// # Examples
4147+
///
4148+
/// ```
4149+
/// use itertools::Itertools;
4150+
/// use itertools::MinMaxResult::{MinMax, NoElements, OneElement};
4151+
///
4152+
/// let abs_cmp = |x: &&i32, y: &&i32| x.abs().cmp(&y.abs());
4153+
///
4154+
/// let a: [i32; 0] = [];
4155+
/// assert_eq!(a.iter().minmax_by(&abs_cmp), NoElements);
4156+
///
4157+
/// let a = [1];
4158+
/// assert_eq!(a.iter().minmax_by(&abs_cmp), OneElement(&1));
4159+
///
4160+
/// let a = [-2, -1, 0, 1, 2];
4161+
/// assert_eq!(a.iter().minmax_by(&abs_cmp), MinMax(&0, &2));
4162+
///
4163+
/// let a = [1, 1, 1, 1];
4164+
/// assert_eq!(a.iter().minmax_by(&abs_cmp), MinMax(&1, &1));
4165+
/// ```
41254166
fn minmax_by<F>(self, mut compare: F) -> MinMaxResult<Self::Item>
41264167
where
41274168
Self: Sized,

0 commit comments

Comments
 (0)