Skip to content

Commit e7a3ada

Browse files
committed
Mention float workaround in Iterator::{min,max}
1 parent f6a28aa commit e7a3ada

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

library/core/src/iter/traits/iterator.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2602,6 +2602,18 @@ pub trait Iterator {
26022602
/// If several elements are equally maximum, the last element is
26032603
/// returned. If the iterator is empty, [`None`] is returned.
26042604
///
2605+
/// Note that [`f32`]/[`f64`] doesn't implement [`Ord`] due to NaN being
2606+
/// incomparable. You can work around this by using [`Iterator::reduce`]:
2607+
/// ```
2608+
/// assert_eq!(
2609+
/// vec![2.4, f32::NAN, 1.3]
2610+
/// .into_iter()
2611+
/// .reduce(|a, b| f32::max(a, b))
2612+
/// .unwrap(),
2613+
/// 2.4
2614+
/// );
2615+
/// ```
2616+
///
26052617
/// # Examples
26062618
///
26072619
/// Basic usage:
@@ -2625,8 +2637,20 @@ pub trait Iterator {
26252637

26262638
/// Returns the minimum element of an iterator.
26272639
///
2628-
/// If several elements are equally minimum, the first element is
2629-
/// returned. If the iterator is empty, [`None`] is returned.
2640+
/// If several elements are equally minimum, the first element is returned.
2641+
/// If the iterator is empty, [`None`] is returned.
2642+
///
2643+
/// Note that [`f32`]/[`f64`] doesn't implement [`Ord`] due to NaN being
2644+
/// incomparable. You can work around this by using [`Iterator::reduce`]:
2645+
/// ```
2646+
/// assert_eq!(
2647+
/// vec![2.4, f32::NAN, 1.3]
2648+
/// .into_iter()
2649+
/// .reduce(|a, b| f32::min(a, b))
2650+
/// .unwrap(),
2651+
/// 1.3
2652+
/// );
2653+
/// ```
26302654
///
26312655
/// # Examples
26322656
///

0 commit comments

Comments
 (0)