/// ```
#[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")]
///
/// Returns the first argument if the comparison determines them to be equal.
///
+/// Internally uses an alias to `Ord::min`.
+///
/// # Examples
///
/// ```
#[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
///
/// ```
#[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
assert_eq!((&mut 12).cmp(&&mut -5), Greater);
}
+#[test]
+fn test_ord_max_min() {
+ assert_eq!(1.max(2), 2);
+ assert_eq!(2.max(1), 2);
+ assert_eq!(1.min(2), 1);
+ assert_eq!(2.min(1), 1);
+ assert_eq!(1.max(1), 1);
+ assert_eq!(1.min(1), 1);
+}
+
#[test]
fn test_ordering_reverse() {
assert_eq!(Less.reverse(), Greater);