From e7a3ada210727a6df1de38299e2177057ff43cef Mon Sep 17 00:00:00 2001 From: Smitty Date: Wed, 26 May 2021 17:15:54 -0400 Subject: [PATCH 1/3] Mention float workaround in Iterator::{min,max} --- library/core/src/iter/traits/iterator.rs | 28 ++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs index 1eef0f9064c90..fcb14e9b77294 100644 --- a/library/core/src/iter/traits/iterator.rs +++ b/library/core/src/iter/traits/iterator.rs @@ -2602,6 +2602,18 @@ pub trait Iterator { /// If several elements are equally maximum, the last element is /// returned. If the iterator is empty, [`None`] is returned. /// + /// Note that [`f32`]/[`f64`] doesn't implement [`Ord`] due to NaN being + /// incomparable. You can work around this by using [`Iterator::reduce`]: + /// ``` + /// assert_eq!( + /// vec![2.4, f32::NAN, 1.3] + /// .into_iter() + /// .reduce(|a, b| f32::max(a, b)) + /// .unwrap(), + /// 2.4 + /// ); + /// ``` + /// /// # Examples /// /// Basic usage: @@ -2625,8 +2637,20 @@ pub trait Iterator { /// Returns the minimum element of an iterator. /// - /// If several elements are equally minimum, the first element is - /// returned. If the iterator is empty, [`None`] is returned. + /// If several elements are equally minimum, the first element is returned. + /// If the iterator is empty, [`None`] is returned. + /// + /// Note that [`f32`]/[`f64`] doesn't implement [`Ord`] due to NaN being + /// incomparable. You can work around this by using [`Iterator::reduce`]: + /// ``` + /// assert_eq!( + /// vec![2.4, f32::NAN, 1.3] + /// .into_iter() + /// .reduce(|a, b| f32::min(a, b)) + /// .unwrap(), + /// 1.3 + /// ); + /// ``` /// /// # Examples /// From 7146a05a43c0baa6bd566ce614491535a6ba4ca2 Mon Sep 17 00:00:00 2001 From: Smittyvb Date: Wed, 26 May 2021 20:38:43 -0400 Subject: [PATCH 2/3] don't use unneeded closure Co-authored-by: Alphyr <47725341+a1phyr@users.noreply.github.com> --- library/core/src/iter/traits/iterator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs index fcb14e9b77294..b07296f8878a0 100644 --- a/library/core/src/iter/traits/iterator.rs +++ b/library/core/src/iter/traits/iterator.rs @@ -2608,7 +2608,7 @@ pub trait Iterator { /// assert_eq!( /// vec![2.4, f32::NAN, 1.3] /// .into_iter() - /// .reduce(|a, b| f32::max(a, b)) + /// .reduce(f32::max) /// .unwrap(), /// 2.4 /// ); From b00f6fc8a16656391f9014cda73b24712eaf2ccb Mon Sep 17 00:00:00 2001 From: Smittyvb Date: Wed, 26 May 2021 20:38:50 -0400 Subject: [PATCH 3/3] don't use unneeded closure Co-authored-by: Alphyr <47725341+a1phyr@users.noreply.github.com> --- library/core/src/iter/traits/iterator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs index b07296f8878a0..556576f3171a0 100644 --- a/library/core/src/iter/traits/iterator.rs +++ b/library/core/src/iter/traits/iterator.rs @@ -2646,7 +2646,7 @@ pub trait Iterator { /// assert_eq!( /// vec![2.4, f32::NAN, 1.3] /// .into_iter() - /// .reduce(|a, b| f32::min(a, b)) + /// .reduce(f32::min) /// .unwrap(), /// 1.3 /// );