@@ -973,6 +973,24 @@ pub trait Ord: Eq + PartialOrd<Self> {
973
973
/// assert_eq!(1.max(2), 2);
974
974
/// assert_eq!(2.max(2), 2);
975
975
/// ```
976
+ /// ```
977
+ /// use std::cmp::Ordering;
978
+ ///
979
+ /// #[derive(Eq)]
980
+ /// struct Equal(&'static str);
981
+ ///
982
+ /// impl PartialEq for Equal {
983
+ /// fn eq(&self, other: &Self) -> bool { true }
984
+ /// }
985
+ /// impl PartialOrd for Equal {
986
+ /// fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(Ordering::Equal) }
987
+ /// }
988
+ /// impl Ord for Equal {
989
+ /// fn cmp(&self, other: &Self) -> Ordering { Ordering::Equal }
990
+ /// }
991
+ ///
992
+ /// assert_eq!(Equal("self").max(Equal("other")).0, "other");
993
+ /// ```
976
994
#[ stable( feature = "ord_max_min" , since = "1.21.0" ) ]
977
995
#[ inline]
978
996
#[ must_use]
@@ -981,7 +999,7 @@ pub trait Ord: Eq + PartialOrd<Self> {
981
999
where
982
1000
Self : Sized ,
983
1001
{
984
- max_by ( self , other, Ord :: cmp )
1002
+ if other < self { self } else { other }
985
1003
}
986
1004
987
1005
/// Compares and returns the minimum of two values.
@@ -994,6 +1012,24 @@ pub trait Ord: Eq + PartialOrd<Self> {
994
1012
/// assert_eq!(1.min(2), 1);
995
1013
/// assert_eq!(2.min(2), 2);
996
1014
/// ```
1015
+ /// ```
1016
+ /// use std::cmp::Ordering;
1017
+ ///
1018
+ /// #[derive(Eq)]
1019
+ /// struct Equal(&'static str);
1020
+ ///
1021
+ /// impl PartialEq for Equal {
1022
+ /// fn eq(&self, other: &Self) -> bool { true }
1023
+ /// }
1024
+ /// impl PartialOrd for Equal {
1025
+ /// fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(Ordering::Equal) }
1026
+ /// }
1027
+ /// impl Ord for Equal {
1028
+ /// fn cmp(&self, other: &Self) -> Ordering { Ordering::Equal }
1029
+ /// }
1030
+ ///
1031
+ /// assert_eq!(Equal("self").min(Equal("other")).0, "self");
1032
+ /// ```
997
1033
#[ stable( feature = "ord_max_min" , since = "1.21.0" ) ]
998
1034
#[ inline]
999
1035
#[ must_use]
@@ -1002,7 +1038,7 @@ pub trait Ord: Eq + PartialOrd<Self> {
1002
1038
where
1003
1039
Self : Sized ,
1004
1040
{
1005
- min_by ( self , other, Ord :: cmp )
1041
+ if other < self { other } else { self }
1006
1042
}
1007
1043
1008
1044
/// Restrict a value to a certain interval.
@@ -1414,6 +1450,24 @@ pub macro PartialOrd($item:item) {
1414
1450
/// assert_eq!(cmp::min(1, 2), 1);
1415
1451
/// assert_eq!(cmp::min(2, 2), 2);
1416
1452
/// ```
1453
+ /// ```
1454
+ /// use std::cmp::{self, Ordering};
1455
+ ///
1456
+ /// #[derive(Eq)]
1457
+ /// struct Equal(&'static str);
1458
+ ///
1459
+ /// impl PartialEq for Equal {
1460
+ /// fn eq(&self, other: &Self) -> bool { true }
1461
+ /// }
1462
+ /// impl PartialOrd for Equal {
1463
+ /// fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(Ordering::Equal) }
1464
+ /// }
1465
+ /// impl Ord for Equal {
1466
+ /// fn cmp(&self, other: &Self) -> Ordering { Ordering::Equal }
1467
+ /// }
1468
+ ///
1469
+ /// assert_eq!(cmp::min(Equal("v1"), Equal("v2")).0, "v1");
1470
+ /// ```
1417
1471
#[ inline]
1418
1472
#[ must_use]
1419
1473
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -1431,20 +1485,22 @@ pub fn min<T: Ord>(v1: T, v2: T) -> T {
1431
1485
/// ```
1432
1486
/// use std::cmp;
1433
1487
///
1434
- /// let result = cmp::min_by(-2, 1, |x: &i32, y: &i32| x.abs().cmp(&y.abs()));
1435
- /// assert_eq!(result, 1);
1488
+ /// let abs_cmp = |x: &i32, y: &i32| x.abs().cmp(&y.abs());
1436
1489
///
1437
- /// let result = cmp::min_by(-2, 3, |x: &i32, y: &i32| x.abs().cmp(&y.abs()));
1438
- /// assert_eq!(result, -2);
1490
+ /// let result = cmp::min_by(2, -1, abs_cmp);
1491
+ /// assert_eq!(result, -1);
1492
+ ///
1493
+ /// let result = cmp::min_by(2, -3, abs_cmp);
1494
+ /// assert_eq!(result, 2);
1495
+ ///
1496
+ /// let result = cmp::min_by(1, -1, abs_cmp);
1497
+ /// assert_eq!(result, 1);
1439
1498
/// ```
1440
1499
#[ inline]
1441
1500
#[ must_use]
1442
1501
#[ stable( feature = "cmp_min_max_by" , since = "1.53.0" ) ]
1443
1502
pub fn min_by < T , F : FnOnce ( & T , & T ) -> Ordering > ( v1 : T , v2 : T , compare : F ) -> T {
1444
- match compare ( & v1, & v2) {
1445
- Ordering :: Less | Ordering :: Equal => v1,
1446
- Ordering :: Greater => v2,
1447
- }
1503
+ if compare ( & v2, & v1) . is_lt ( ) { v2 } else { v1 }
1448
1504
}
1449
1505
1450
1506
/// Returns the element that gives the minimum value from the specified function.
@@ -1456,17 +1512,20 @@ pub fn min_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
1456
1512
/// ```
1457
1513
/// use std::cmp;
1458
1514
///
1459
- /// let result = cmp::min_by_key(- 2, 1, |x: &i32| x.abs());
1460
- /// assert_eq!(result, 1);
1515
+ /// let result = cmp::min_by_key(2, - 1, |x: &i32| x.abs());
1516
+ /// assert_eq!(result, - 1);
1461
1517
///
1462
- /// let result = cmp::min_by_key(-2, 2, |x: &i32| x.abs());
1463
- /// assert_eq!(result, -2);
1518
+ /// let result = cmp::min_by_key(2, -3, |x: &i32| x.abs());
1519
+ /// assert_eq!(result, 2);
1520
+ ///
1521
+ /// let result = cmp::min_by_key(1, -1, |x: &i32| x.abs());
1522
+ /// assert_eq!(result, 1);
1464
1523
/// ```
1465
1524
#[ inline]
1466
1525
#[ must_use]
1467
1526
#[ stable( feature = "cmp_min_max_by" , since = "1.53.0" ) ]
1468
1527
pub fn min_by_key < T , F : FnMut ( & T ) -> K , K : Ord > ( v1 : T , v2 : T , mut f : F ) -> T {
1469
- min_by ( v1 , v2 , |v1 , v2| f ( v1) . cmp ( & f ( v2 ) ) )
1528
+ if f ( & v2 ) < f ( & v1) { v2 } else { v1 }
1470
1529
}
1471
1530
1472
1531
/// Compares and returns the maximum of two values.
@@ -1483,6 +1542,24 @@ pub fn min_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
1483
1542
/// assert_eq!(cmp::max(1, 2), 2);
1484
1543
/// assert_eq!(cmp::max(2, 2), 2);
1485
1544
/// ```
1545
+ /// ```
1546
+ /// use std::cmp::{self, Ordering};
1547
+ ///
1548
+ /// #[derive(Eq)]
1549
+ /// struct Equal(&'static str);
1550
+ ///
1551
+ /// impl PartialEq for Equal {
1552
+ /// fn eq(&self, other: &Self) -> bool { true }
1553
+ /// }
1554
+ /// impl PartialOrd for Equal {
1555
+ /// fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(Ordering::Equal) }
1556
+ /// }
1557
+ /// impl Ord for Equal {
1558
+ /// fn cmp(&self, other: &Self) -> Ordering { Ordering::Equal }
1559
+ /// }
1560
+ ///
1561
+ /// assert_eq!(cmp::max(Equal("v1"), Equal("v2")).0, "v2");
1562
+ /// ```
1486
1563
#[ inline]
1487
1564
#[ must_use]
1488
1565
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -1500,20 +1577,22 @@ pub fn max<T: Ord>(v1: T, v2: T) -> T {
1500
1577
/// ```
1501
1578
/// use std::cmp;
1502
1579
///
1503
- /// let result = cmp::max_by(-2, 1, |x: &i32, y: &i32| x.abs().cmp(&y.abs()));
1580
+ /// let abs_cmp = |x: &i32, y: &i32| x.abs().cmp(&y.abs());
1581
+ ///
1582
+ /// let result = cmp::max_by(3, -2, abs_cmp) ;
1583
+ /// assert_eq!(result, 3);
1584
+ ///
1585
+ /// let result = cmp::max_by(1, -2, abs_cmp);
1504
1586
/// assert_eq!(result, -2);
1505
1587
///
1506
- /// let result = cmp::max_by(-2, 2, |x: &i32, y: &i32| x.abs().cmp(&y.abs())) ;
1507
- /// assert_eq!(result, 2 );
1588
+ /// let result = cmp::max_by(1, -1, abs_cmp) ;
1589
+ /// assert_eq!(result, -1 );
1508
1590
/// ```
1509
1591
#[ inline]
1510
1592
#[ must_use]
1511
1593
#[ stable( feature = "cmp_min_max_by" , since = "1.53.0" ) ]
1512
1594
pub fn max_by < T , F : FnOnce ( & T , & T ) -> Ordering > ( v1 : T , v2 : T , compare : F ) -> T {
1513
- match compare ( & v1, & v2) {
1514
- Ordering :: Less | Ordering :: Equal => v2,
1515
- Ordering :: Greater => v1,
1516
- }
1595
+ if compare ( & v2, & v1) . is_lt ( ) { v1 } else { v2 }
1517
1596
}
1518
1597
1519
1598
/// Returns the element that gives the maximum value from the specified function.
@@ -1525,17 +1604,20 @@ pub fn max_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
1525
1604
/// ```
1526
1605
/// use std::cmp;
1527
1606
///
1528
- /// let result = cmp::max_by_key(-2, 1, |x: &i32| x.abs());
1607
+ /// let result = cmp::max_by_key(3, -2, |x: &i32| x.abs());
1608
+ /// assert_eq!(result, 3);
1609
+ ///
1610
+ /// let result = cmp::max_by_key(1, -2, |x: &i32| x.abs());
1529
1611
/// assert_eq!(result, -2);
1530
1612
///
1531
- /// let result = cmp::max_by_key(-2, 2 , |x: &i32| x.abs());
1532
- /// assert_eq!(result, 2 );
1613
+ /// let result = cmp::max_by_key(1, -1 , |x: &i32| x.abs());
1614
+ /// assert_eq!(result, -1 );
1533
1615
/// ```
1534
1616
#[ inline]
1535
1617
#[ must_use]
1536
1618
#[ stable( feature = "cmp_min_max_by" , since = "1.53.0" ) ]
1537
1619
pub fn max_by_key < T , F : FnMut ( & T ) -> K , K : Ord > ( v1 : T , v2 : T , mut f : F ) -> T {
1538
- max_by ( v1 , v2 , |v1 , v2| f ( v1) . cmp ( & f ( v2 ) ) )
1620
+ if f ( & v2 ) < f ( & v1) { v1 } else { v2 }
1539
1621
}
1540
1622
1541
1623
/// Compares and sorts two values, returning minimum and maximum.
@@ -1549,21 +1631,40 @@ pub fn max_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
1549
1631
/// use std::cmp;
1550
1632
///
1551
1633
/// assert_eq!(cmp::minmax(1, 2), [1, 2]);
1552
- /// assert_eq!(cmp::minmax(2, 2 ), [2 , 2]);
1634
+ /// assert_eq!(cmp::minmax(2, 1 ), [1 , 2]);
1553
1635
///
1554
1636
/// // You can destructure the result using array patterns
1555
1637
/// let [min, max] = cmp::minmax(42, 17);
1556
1638
/// assert_eq!(min, 17);
1557
1639
/// assert_eq!(max, 42);
1558
1640
/// ```
1641
+ /// ```
1642
+ /// #![feature(cmp_minmax)]
1643
+ /// use std::cmp::{self, Ordering};
1644
+ ///
1645
+ /// #[derive(Eq)]
1646
+ /// struct Equal(&'static str);
1647
+ ///
1648
+ /// impl PartialEq for Equal {
1649
+ /// fn eq(&self, other: &Self) -> bool { true }
1650
+ /// }
1651
+ /// impl PartialOrd for Equal {
1652
+ /// fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(Ordering::Equal) }
1653
+ /// }
1654
+ /// impl Ord for Equal {
1655
+ /// fn cmp(&self, other: &Self) -> Ordering { Ordering::Equal }
1656
+ /// }
1657
+ ///
1658
+ /// assert_eq!(cmp::minmax(Equal("v1"), Equal("v2")).map(|v| v.0), ["v1", "v2"]);
1659
+ /// ```
1559
1660
#[ inline]
1560
1661
#[ must_use]
1561
1662
#[ unstable( feature = "cmp_minmax" , issue = "115939" ) ]
1562
1663
pub fn minmax < T > ( v1 : T , v2 : T ) -> [ T ; 2 ]
1563
1664
where
1564
1665
T : Ord ,
1565
1666
{
1566
- if v1 <= v2 { [ v1 , v2 ] } else { [ v2 , v1 ] }
1667
+ if v2 < v1 { [ v2 , v1 ] } else { [ v1 , v2 ] }
1567
1668
}
1568
1669
1569
1670
/// Returns minimum and maximum values with respect to the specified comparison function.
@@ -1576,11 +1677,14 @@ where
1576
1677
/// #![feature(cmp_minmax)]
1577
1678
/// use std::cmp;
1578
1679
///
1579
- /// assert_eq!(cmp::minmax_by(-2, 1, |x: &i32, y: &i32| x.abs().cmp(&y.abs())), [1, -2]);
1580
- /// assert_eq!(cmp::minmax_by(-2, 2, |x: &i32, y: &i32| x.abs().cmp(&y.abs())), [-2, 2]);
1680
+ /// let abs_cmp = |x: &i32, y: &i32| x.abs().cmp(&y.abs());
1681
+ ///
1682
+ /// assert_eq!(cmp::minmax_by(-2, 1, abs_cmp), [1, -2]);
1683
+ /// assert_eq!(cmp::minmax_by(-1, 2, abs_cmp), [-1, 2]);
1684
+ /// assert_eq!(cmp::minmax_by(-2, 2, abs_cmp), [-2, 2]);
1581
1685
///
1582
1686
/// // You can destructure the result using array patterns
1583
- /// let [min, max] = cmp::minmax_by(-42, 17, |x: &i32, y: &i32| x.abs().cmp(&y.abs()) );
1687
+ /// let [min, max] = cmp::minmax_by(-42, 17, abs_cmp );
1584
1688
/// assert_eq!(min, 17);
1585
1689
/// assert_eq!(max, -42);
1586
1690
/// ```
@@ -1591,7 +1695,7 @@ pub fn minmax_by<T, F>(v1: T, v2: T, compare: F) -> [T; 2]
1591
1695
where
1592
1696
F : FnOnce ( & T , & T ) -> Ordering ,
1593
1697
{
1594
- if compare ( & v1 , & v2 ) . is_le ( ) { [ v1 , v2 ] } else { [ v2 , v1 ] }
1698
+ if compare ( & v2 , & v1 ) . is_lt ( ) { [ v2 , v1 ] } else { [ v1 , v2 ] }
1595
1699
}
1596
1700
1597
1701
/// Returns minimum and maximum values with respect to the specified key function.
@@ -1620,7 +1724,7 @@ where
1620
1724
F : FnMut ( & T ) -> K ,
1621
1725
K : Ord ,
1622
1726
{
1623
- minmax_by ( v1 , v2, |v1 , v2| f ( v1 ) . cmp ( & f ( v2 ) ) )
1727
+ if f ( & v2 ) < f ( & v1 ) { [ v2, v1 ] } else { [ v1 , v2 ] }
1624
1728
}
1625
1729
1626
1730
// Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types
0 commit comments