@@ -1552,6 +1552,9 @@ pub fn min<T: Ord>(v1: T, v2: T) -> T {
1552
1552
///
1553
1553
/// Returns the first argument if the comparison determines them to be equal.
1554
1554
///
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
+ ///
1555
1558
/// # Examples
1556
1559
///
1557
1560
/// ```
@@ -1567,12 +1570,17 @@ pub fn min<T: Ord>(v1: T, v2: T) -> T {
1567
1570
///
1568
1571
/// let result = cmp::min_by(1, -1, abs_cmp);
1569
1572
/// 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);
1570
1578
/// ```
1571
1579
#[ inline]
1572
1580
#[ must_use]
1573
1581
#[ stable( feature = "cmp_min_max_by" , since = "1.53.0" ) ]
1574
1582
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 }
1576
1584
}
1577
1585
1578
1586
/// 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 {
1644
1652
///
1645
1653
/// Returns the second argument if the comparison determines them to be equal.
1646
1654
///
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
+ ///
1647
1658
/// # Examples
1648
1659
///
1649
1660
/// ```
@@ -1659,12 +1670,17 @@ pub fn max<T: Ord>(v1: T, v2: T) -> T {
1659
1670
///
1660
1671
/// let result = cmp::max_by(1, -1, abs_cmp);
1661
1672
/// 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);
1662
1678
/// ```
1663
1679
#[ inline]
1664
1680
#[ must_use]
1665
1681
#[ stable( feature = "cmp_min_max_by" , since = "1.53.0" ) ]
1666
1682
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 }
1668
1684
}
1669
1685
1670
1686
/// Returns the element that gives the maximum value from the specified function.
@@ -1743,6 +1759,9 @@ where
1743
1759
///
1744
1760
/// Returns `[v1, v2]` if the comparison determines them to be equal.
1745
1761
///
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
+ ///
1746
1765
/// # Examples
1747
1766
///
1748
1767
/// ```
@@ -1755,6 +1774,10 @@ where
1755
1774
/// assert_eq!(cmp::minmax_by(-1, 2, abs_cmp), [-1, 2]);
1756
1775
/// assert_eq!(cmp::minmax_by(-2, 2, abs_cmp), [-2, 2]);
1757
1776
///
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
+ ///
1758
1781
/// // You can destructure the result using array patterns
1759
1782
/// let [min, max] = cmp::minmax_by(-42, 17, abs_cmp);
1760
1783
/// assert_eq!(min, 17);
@@ -1767,7 +1790,7 @@ pub fn minmax_by<T, F>(v1: T, v2: T, compare: F) -> [T; 2]
1767
1790
where
1768
1791
F : FnOnce ( & T , & T ) -> Ordering ,
1769
1792
{
1770
- if compare ( & v2 , & v1 ) . is_lt ( ) { [ v2 , v1 ] } else { [ v1 , v2 ] }
1793
+ if compare ( & v1 , & v2 ) . is_le ( ) { [ v1 , v2 ] } else { [ v2 , v1 ] }
1771
1794
}
1772
1795
1773
1796
/// Returns minimum and maximum values with respect to the specified key function.
0 commit comments