summary refs log tree commit diff
path: root/src/libcore/cmp.rs
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2015-03-31 10:15:32 -0700
committerAlex Crichton <alex@alexcrichton.com>2015-03-31 15:49:11 -0700
commitf6bd6b4f3dca5cf5b24c8ed89c7b65b4a89104ce (patch)
treeba1c3cc58dcc580a3808d343bf991d5804059a3c /src/libcore/cmp.rs
parentd03120afd36f9e3f4c1305e9e5ca0d24a3b4a32f (diff)
parent2a89d695f4edb6fe8c7629f48908d3d3d9076426 (diff)
downloadrust-f6bd6b4f3dca5cf5b24c8ed89c7b65b4a89104ce.tar.gz
rust-f6bd6b4f3dca5cf5b24c8ed89c7b65b4a89104ce.zip
rollup merge of #23878: Ryman/stable_extremes
`min`-like functions now return the leftmost element/input for equal elements.
`max`-like return the rightmost.

Closes #23687.

cc @HeroesGrave, @aturon, @alexcrichton
Diffstat (limited to 'src/libcore/cmp.rs')
-rw-r--r--src/libcore/cmp.rs14
1 files changed, 9 insertions, 5 deletions
diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs
index 9b8b59ec8ce..49dd108b89c 100644
--- a/src/libcore/cmp.rs
+++ b/src/libcore/cmp.rs
@@ -360,6 +360,8 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
 
 /// Compare and return the minimum of two values.
 ///
+/// Returns the first argument if the comparison determines them to be equal.
+///
 /// # Examples
 ///
 /// ```
@@ -371,11 +373,13 @@ 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 }
+    if v1 <= v2 { v1 } else { v2 }
 }
 
 /// Compare and return the maximum of two values.
 ///
+/// Returns the second argument if the comparison determines them to be equal.
+///
 /// # Examples
 ///
 /// ```
@@ -387,7 +391,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 v1 > v2 { v1 } else { v2 }
+    if v2 >= v1 { v2 } else { v1 }
 }
 
 /// Compare and return the minimum of two values if there is one.
@@ -425,7 +429,7 @@ pub fn partial_min<T: PartialOrd>(v1: T, v2: T) -> Option<T> {
 
 /// Compare and return the maximum of two values if there is one.
 ///
-/// Returns the first argument if the comparison determines them to be equal.
+/// Returns the second argument if the comparison determines them to be equal.
 ///
 /// # Examples
 ///
@@ -450,8 +454,8 @@ pub fn partial_min<T: PartialOrd>(v1: T, v2: T) -> Option<T> {
 #[unstable(feature = "core")]
 pub fn partial_max<T: PartialOrd>(v1: T, v2: T) -> Option<T> {
     match v1.partial_cmp(&v2) {
-        Some(Less) => Some(v2),
-        Some(Equal) | Some(Greater) => Some(v1),
+        Some(Equal) | Some(Less) => Some(v2),
+        Some(Greater) => Some(v1),
         None => None
     }
 }