Skip to content

Commit e30192a

Browse files
Rollup merge of rust-lang#85730 - Smittyvb:iter-min-max-floats, r=m-ou-se
Mention workaround for floats in Iterator::{min, max} `Iterator::{min, max}` can't be used with iterators of floats due to NaN issues. This suggests a workaround in the documentation of those functions.
2 parents 6ac83e1 + b00f6fc commit e30192a

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
@@ -2568,6 +2568,18 @@ pub trait Iterator {
25682568
/// If several elements are equally maximum, the last element is
25692569
/// returned. If the iterator is empty, [`None`] is returned.
25702570
///
2571+
/// Note that [`f32`]/[`f64`] doesn't implement [`Ord`] due to NaN being
2572+
/// incomparable. You can work around this by using [`Iterator::reduce`]:
2573+
/// ```
2574+
/// assert_eq!(
2575+
/// vec![2.4, f32::NAN, 1.3]
2576+
/// .into_iter()
2577+
/// .reduce(f32::max)
2578+
/// .unwrap(),
2579+
/// 2.4
2580+
/// );
2581+
/// ```
2582+
///
25712583
/// # Examples
25722584
///
25732585
/// Basic usage:
@@ -2591,8 +2603,20 @@ pub trait Iterator {
25912603

25922604
/// Returns the minimum element of an iterator.
25932605
///
2594-
/// If several elements are equally minimum, the first element is
2595-
/// returned. If the iterator is empty, [`None`] is returned.
2606+
/// If several elements are equally minimum, the first element is returned.
2607+
/// If the iterator is empty, [`None`] is returned.
2608+
///
2609+
/// Note that [`f32`]/[`f64`] doesn't implement [`Ord`] due to NaN being
2610+
/// incomparable. You can work around this by using [`Iterator::reduce`]:
2611+
/// ```
2612+
/// assert_eq!(
2613+
/// vec![2.4, f32::NAN, 1.3]
2614+
/// .into_iter()
2615+
/// .reduce(f32::min)
2616+
/// .unwrap(),
2617+
/// 1.3
2618+
/// );
2619+
/// ```
25962620
///
25972621
/// # Examples
25982622
///

0 commit comments

Comments
 (0)