1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// This file is part of helpers4.
// Copyright (C) 2025 baxyz
// SPDX-License-Identifier: LGPL-3.0-or-later
/// Returns the smallest and largest item of `iter` in one pass, or `None` when it is empty.
///
/// Equivalent to calling
/// [`.min()`](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.min) and
/// [`.max()`](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.max) separately, but
/// only iterates once, so it also works on an iterator that can only be consumed a single time.
/// Comparisons follow `T`'s [`PartialOrd`]: with `f64`, a `NaN` is neither smaller nor larger
/// than anything, exactly as `<` and `>` say, so a `NaN` that is never replaced (for instance the
/// very first item) stays in the result.
///
/// # Arguments
///
/// - `iter` - The items to scan.
///
/// # Returns
///
/// `(min, max)`, or `None` when `iter` is empty.
///
/// # Examples
///
/// ```
/// use helpers4::iter::min_max;
///
/// assert_eq!(min_max(1..=5), Some((1, 5)));
/// assert_eq!(min_max([3, 1, 4, 1, 5]), Some((1, 5)));
/// assert_eq!(min_max(Vec::<i32>::new()), None);
/// ```