Skip to content

Commit f5a4bb6

Browse files
committed
Fix parameter order for _by() variants of min / max/ minmax in std::cmp
1 parent 4c83e55 commit f5a4bb6

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

library/core/src/cmp.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -1567,12 +1567,17 @@ pub fn min<T: Ord>(v1: T, v2: T) -> T {
15671567
///
15681568
/// let result = cmp::min_by(1, -1, abs_cmp);
15691569
/// assert_eq!(result, 1);
1570+
///
1571+
/// let rhs_abs_cmp = |x: &i32, y: &i32| x.cmp(&y.abs());
1572+
///
1573+
/// let result = cmp::min_by(-2, 1, rhs_abs_cmp);
1574+
/// assert_eq!(result, -2);
15701575
/// ```
15711576
#[inline]
15721577
#[must_use]
15731578
#[stable(feature = "cmp_min_max_by", since = "1.53.0")]
15741579
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 }
1580+
if compare(&v1, &v2).is_le() { v1 } else { v2 }
15761581
}
15771582

15781583
/// Returns the element that gives the minimum value from the specified function.
@@ -1659,12 +1664,17 @@ pub fn max<T: Ord>(v1: T, v2: T) -> T {
16591664
///
16601665
/// let result = cmp::max_by(1, -1, abs_cmp);
16611666
/// assert_eq!(result, -1);
1667+
///
1668+
/// let rhs_abs_cmp = |x: &i32, y: &i32| x.cmp(&y.abs());
1669+
///
1670+
/// let result = cmp::max_by(-2, 1, rhs_abs_cmp);
1671+
/// assert_eq!(result, 1);
16621672
/// ```
16631673
#[inline]
16641674
#[must_use]
16651675
#[stable(feature = "cmp_min_max_by", since = "1.53.0")]
16661676
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 }
1677+
if compare(&v1, &v2).is_gt() { v1 } else { v2 }
16681678
}
16691679

16701680
/// Returns the element that gives the maximum value from the specified function.
@@ -1755,6 +1765,10 @@ where
17551765
/// assert_eq!(cmp::minmax_by(-1, 2, abs_cmp), [-1, 2]);
17561766
/// assert_eq!(cmp::minmax_by(-2, 2, abs_cmp), [-2, 2]);
17571767
///
1768+
/// let rhs_abs_cmp = |x: &i32, y: &i32| x.cmp(&y.abs());
1769+
///
1770+
/// assert_eq!(cmp::minmax_by(-2, 1, rhs_abs_cmp), [-2, 1]);
1771+
///
17581772
/// // You can destructure the result using array patterns
17591773
/// let [min, max] = cmp::minmax_by(-42, 17, abs_cmp);
17601774
/// assert_eq!(min, 17);
@@ -1767,7 +1781,7 @@ pub fn minmax_by<T, F>(v1: T, v2: T, compare: F) -> [T; 2]
17671781
where
17681782
F: FnOnce(&T, &T) -> Ordering,
17691783
{
1770-
if compare(&v2, &v1).is_lt() { [v2, v1] } else { [v1, v2] }
1784+
if compare(&v1, &v2).is_le() { [v1, v2] } else { [v2, v1] }
17711785
}
17721786

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

0 commit comments

Comments
 (0)