@@ -1567,12 +1567,17 @@ pub fn min<T: Ord>(v1: T, v2: T) -> T {
1567
1567
///
1568
1568
/// let result = cmp::min_by(1, -1, abs_cmp);
1569
1569
/// 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);
1570
1575
/// ```
1571
1576
#[ inline]
1572
1577
#[ must_use]
1573
1578
#[ stable( feature = "cmp_min_max_by" , since = "1.53.0" ) ]
1574
1579
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 }
1576
1581
}
1577
1582
1578
1583
/// 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 {
1659
1664
///
1660
1665
/// let result = cmp::max_by(1, -1, abs_cmp);
1661
1666
/// 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);
1662
1672
/// ```
1663
1673
#[ inline]
1664
1674
#[ must_use]
1665
1675
#[ stable( feature = "cmp_min_max_by" , since = "1.53.0" ) ]
1666
1676
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 }
1668
1678
}
1669
1679
1670
1680
/// Returns the element that gives the maximum value from the specified function.
@@ -1755,6 +1765,10 @@ where
1755
1765
/// assert_eq!(cmp::minmax_by(-1, 2, abs_cmp), [-1, 2]);
1756
1766
/// assert_eq!(cmp::minmax_by(-2, 2, abs_cmp), [-2, 2]);
1757
1767
///
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
+ ///
1758
1772
/// // You can destructure the result using array patterns
1759
1773
/// let [min, max] = cmp::minmax_by(-42, 17, abs_cmp);
1760
1774
/// assert_eq!(min, 17);
@@ -1767,7 +1781,7 @@ pub fn minmax_by<T, F>(v1: T, v2: T, compare: F) -> [T; 2]
1767
1781
where
1768
1782
F : FnOnce ( & T , & T ) -> Ordering ,
1769
1783
{
1770
- if compare ( & v2 , & v1 ) . is_lt ( ) { [ v2 , v1 ] } else { [ v1 , v2 ] }
1784
+ if compare ( & v1 , & v2 ) . is_le ( ) { [ v1 , v2 ] } else { [ v2 , v1 ] }
1771
1785
}
1772
1786
1773
1787
/// Returns minimum and maximum values with respect to the specified key function.
0 commit comments