@@ -1266,6 +1266,91 @@ pub fn max_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
12661266 max_by ( v1, v2, |v1, v2| f ( v1) . cmp ( & f ( v2) ) )
12671267}
12681268
1269+ /// Compares and sorts two values, returning minimum and maximum.
1270+ ///
1271+ /// Returns `[v1, v2]` if the comparison determines them to be equal.
1272+ ///
1273+ /// # Examples
1274+ ///
1275+ /// ```
1276+ /// #![feature(cmp_minmax)]
1277+ /// use std::cmp;
1278+ ///
1279+ /// assert_eq!(cmp::minmax(1, 2), [1, 2]);
1280+ /// assert_eq!(cmp::minmax(2, 2), [2, 2]);
1281+ ///
1282+ /// // You can destructure the result using array patterns
1283+ /// let [min, max] = cmp::minmax(42, 17);
1284+ /// assert_eq!(min, 17);
1285+ /// assert_eq!(max, 42);
1286+ /// ```
1287+ #[ inline]
1288+ #[ must_use]
1289+ #[ unstable( feature = "cmp_minmax" , issue = "none" ) ]
1290+ pub fn minmax < T > ( v1 : T , v2 : T ) -> [ T ; 2 ]
1291+ where
1292+ T : Ord ,
1293+ {
1294+ if v1 <= v2 { [ v1, v2] } else { [ v2, v1] }
1295+ }
1296+
1297+ /// Returns minimum and maximum values with respect to the specified comparison function.
1298+ ///
1299+ /// Returns `[v1, v2]` if the comparison determines them to be equal.
1300+ ///
1301+ /// # Examples
1302+ ///
1303+ /// ```
1304+ /// #![feature(cmp_minmax)]
1305+ /// use std::cmp;
1306+ ///
1307+ /// assert_eq!(cmp::minmax_by(-2, 1, |x: &i32, y: &i32| x.abs().cmp(&y.abs())), [1, -2]);
1308+ /// assert_eq!(cmp::minmax_by(-2, 2, |x: &i32, y: &i32| x.abs().cmp(&y.abs())), [-2, 2]);
1309+ ///
1310+ /// // You can destructure the result using array patterns
1311+ /// let [min, max] = cmp::minmax_by(-42, 17, |x: &i32, y: &i32| x.abs().cmp(&y.abs()));
1312+ /// assert_eq!(min, 17);
1313+ /// assert_eq!(max, -42);
1314+ /// ```
1315+ #[ inline]
1316+ #[ must_use]
1317+ #[ unstable( feature = "cmp_minmax" , issue = "none" ) ]
1318+ pub fn minmax_by < T , F > ( v1 : T , v2 : T , compare : F ) -> [ T ; 2 ]
1319+ where
1320+ F : FnOnce ( & T , & T ) -> Ordering ,
1321+ {
1322+ if compare ( & v1, & v2) . is_le ( ) { [ v1, v2] } else { [ v2, v1] }
1323+ }
1324+
1325+ /// Returns minimum and maximum values with respect to the specified key function.
1326+ ///
1327+ /// Returns `[v1, v2]` if the comparison determines them to be equal.
1328+ ///
1329+ /// # Examples
1330+ ///
1331+ /// ```
1332+ /// #![feature(cmp_minmax)]
1333+ /// use std::cmp;
1334+ ///
1335+ /// assert_eq!(cmp::minmax_by_key(-2, 1, |x: &i32| x.abs()), [1, -2]);
1336+ /// assert_eq!(cmp::minmax_by_key(-2, 2, |x: &i32| x.abs()), [-2, 2]);
1337+ ///
1338+ /// // You can destructure the result using array patterns
1339+ /// let [min, max] = cmp::minmax_by_key(-42, 17, |x: &i32| x.abs());
1340+ /// assert_eq!(min, 17);
1341+ /// assert_eq!(max, -42);
1342+ /// ```
1343+ #[ inline]
1344+ #[ must_use]
1345+ #[ unstable( feature = "cmp_minmax" , issue = "none" ) ]
1346+ pub fn minmax_by_key < T , F , K > ( v1 : T , v2 : T , mut f : F ) -> [ T ; 2 ]
1347+ where
1348+ F : FnMut ( & T ) -> K ,
1349+ K : Ord ,
1350+ {
1351+ minmax_by ( v1, v2, |v1, v2| f ( v1) . cmp ( & f ( v2) ) )
1352+ }
1353+
12691354// Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types
12701355mod impls {
12711356 use crate :: cmp:: Ordering :: { self , Equal , Greater , Less } ;
0 commit comments