diff options
Diffstat (limited to 'src/libcore/cmp.rs')
| -rw-r--r-- | src/libcore/cmp.rs | 44 |
1 files changed, 42 insertions, 2 deletions
diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index 661cf73c7f3..6f35d0417f1 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -443,6 +443,42 @@ pub trait Ord: Eq + PartialOrd<Self> { /// ``` #[stable(feature = "rust1", since = "1.0.0")] fn cmp(&self, other: &Self) -> Ordering; + + /// Compares and returns the maximum of two values. + /// + /// Returns the second argument if the comparison determines them to be equal. + /// + /// # Examples + /// + /// ``` + /// #![feature(ord_max_min)] + /// + /// assert_eq!(2, 1.max(2)); + /// assert_eq!(2, 2.max(2)); + /// ``` + #[unstable(feature = "ord_max_min", issue = "25663")] + fn max(self, other: Self) -> Self + where Self: Sized { + if other >= self { other } else { self } + } + + /// Compares and returns the minimum of two values. + /// + /// Returns the first argument if the comparison determines them to be equal. + /// + /// # Examples + /// + /// ``` + /// #![feature(ord_max_min)] + /// + /// assert_eq!(1, 1.min(2)); + /// assert_eq!(2, 2.min(2)); + /// ``` + #[unstable(feature = "ord_max_min", issue = "25663")] + fn min(self, other: Self) -> Self + where Self: Sized { + if self <= other { self } else { other } + } } #[stable(feature = "rust1", since = "1.0.0")] @@ -678,6 +714,8 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> { /// /// Returns the first argument if the comparison determines them to be equal. /// +/// Internally uses an alias to `Ord::min`. +/// /// # Examples /// /// ``` @@ -689,13 +727,15 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> { #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub fn min<T: Ord>(v1: T, v2: T) -> T { - if v1 <= v2 { v1 } else { v2 } + v1.min(v2) } /// Compares and returns the maximum of two values. /// /// Returns the second argument if the comparison determines them to be equal. /// +/// Internally uses an alias to `Ord::max`. +/// /// # Examples /// /// ``` @@ -707,7 +747,7 @@ pub fn min<T: Ord>(v1: T, v2: T) -> T { #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub fn max<T: Ord>(v1: T, v2: T) -> T { - if v2 >= v1 { v2 } else { v1 } + v1.max(v2) } // Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types |
