Skip to content

Commit 1ddb4d0

Browse files
committed
Fix parameter order for _by() variants of min / max/ minmax in std::cmp
1 parent 54d024e commit 1ddb4d0

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

library/core/src/cmp.rs

+26-3
Original file line numberDiff line numberDiff line change
@@ -1552,6 +1552,9 @@ pub fn min<T: Ord>(v1: T, v2: T) -> T {
15521552
///
15531553
/// Returns the first argument if the comparison determines them to be equal.
15541554
///
1555+
/// The parameter order is preserved when calling the `compare` function, i.e. `v1` is
1556+
/// always passed as the first argument and `v2` as the second.
1557+
///
15551558
/// # Examples
15561559
///
15571560
/// ```
@@ -1567,12 +1570,17 @@ pub fn min<T: Ord>(v1: T, v2: T) -> T {
15671570
///
15681571
/// let result = cmp::min_by(1, -1, abs_cmp);
15691572
/// assert_eq!(result, 1);
1573+
///
1574+
/// let rhs_abs_cmp = |x: &i32, y: &i32| x.cmp(&y.abs());
1575+
///
1576+
/// let result = cmp::min_by(-2, 1, rhs_abs_cmp);
1577+
/// assert_eq!(result, -2);
15701578
/// ```
15711579
#[inline]
15721580
#[must_use]
15731581
#[stable(feature = "cmp_min_max_by", since = "1.53.0")]
15741582
pub fn min_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
1575-
if compare(&v2, &v1).is_lt() { v2 } else { v1 }
1583+
if compare(&v1, &v2).is_le() { v1 } else { v2 }
15761584
}
15771585

15781586
/// Returns the element that gives the minimum value from the specified function.
@@ -1644,6 +1652,9 @@ pub fn max<T: Ord>(v1: T, v2: T) -> T {
16441652
///
16451653
/// Returns the second argument if the comparison determines them to be equal.
16461654
///
1655+
/// The parameter order is preserved when calling the `compare` function, i.e. `v1` is
1656+
/// always passed as the first argument and `v2` as the second.
1657+
///
16471658
/// # Examples
16481659
///
16491660
/// ```
@@ -1659,12 +1670,17 @@ pub fn max<T: Ord>(v1: T, v2: T) -> T {
16591670
///
16601671
/// let result = cmp::max_by(1, -1, abs_cmp);
16611672
/// assert_eq!(result, -1);
1673+
///
1674+
/// let rhs_abs_cmp = |x: &i32, y: &i32| x.cmp(&y.abs());
1675+
///
1676+
/// let result = cmp::max_by(-2, 1, rhs_abs_cmp);
1677+
/// assert_eq!(result, 1);
16621678
/// ```
16631679
#[inline]
16641680
#[must_use]
16651681
#[stable(feature = "cmp_min_max_by", since = "1.53.0")]
16661682
pub fn max_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
1667-
if compare(&v2, &v1).is_lt() { v1 } else { v2 }
1683+
if compare(&v1, &v2).is_gt() { v1 } else { v2 }
16681684
}
16691685

16701686
/// Returns the element that gives the maximum value from the specified function.
@@ -1743,6 +1759,9 @@ where
17431759
///
17441760
/// Returns `[v1, v2]` if the comparison determines them to be equal.
17451761
///
1762+
/// The parameter order is preserved when calling the `compare` function, i.e. `v1` is
1763+
/// always passed as the first argument and `v2` as the second.
1764+
///
17461765
/// # Examples
17471766
///
17481767
/// ```
@@ -1755,6 +1774,10 @@ where
17551774
/// assert_eq!(cmp::minmax_by(-1, 2, abs_cmp), [-1, 2]);
17561775
/// assert_eq!(cmp::minmax_by(-2, 2, abs_cmp), [-2, 2]);
17571776
///
1777+
/// let rhs_abs_cmp = |x: &i32, y: &i32| x.cmp(&y.abs());
1778+
///
1779+
/// assert_eq!(cmp::minmax_by(-2, 1, rhs_abs_cmp), [-2, 1]);
1780+
///
17581781
/// // You can destructure the result using array patterns
17591782
/// let [min, max] = cmp::minmax_by(-42, 17, abs_cmp);
17601783
/// assert_eq!(min, 17);
@@ -1767,7 +1790,7 @@ pub fn minmax_by<T, F>(v1: T, v2: T, compare: F) -> [T; 2]
17671790
where
17681791
F: FnOnce(&T, &T) -> Ordering,
17691792
{
1770-
if compare(&v2, &v1).is_lt() { [v2, v1] } else { [v1, v2] }
1793+
if compare(&v1, &v2).is_le() { [v1, v2] } else { [v2, v1] }
17711794
}
17721795

17731796
/// Returns minimum and maximum values with respect to the specified key function.

0 commit comments

Comments
 (0)