mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-22 02:00:00 +03:00
Rollup merge of #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.
This commit is contained in:
@@ -2568,6 +2568,18 @@ fn check<T>(
|
||||
/// 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(f32::max)
|
||||
/// .unwrap(),
|
||||
/// 2.4
|
||||
/// );
|
||||
/// ```
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Basic usage:
|
||||
@@ -2591,8 +2603,20 @@ fn max(self) -> Option<Self::Item>
|
||||
|
||||
/// 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(f32::min)
|
||||
/// .unwrap(),
|
||||
/// 1.3
|
||||
/// );
|
||||
/// ```
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
||||
Reference in New Issue
Block a user