]> git.lizzy.rs Git - rust.git/blobdiff - library/core/src/num/uint_macros.rs
Added abs_diff for integer types.
[rust.git] / library / core / src / num / uint_macros.rs
index 7523d8ec976ddd137c9d9ba366f96d6504e22106..1bba4179f99552a1b4bbad289c1af0585dc21d63 100644 (file)
@@ -1490,6 +1490,27 @@ pub const fn borrowing_sub(self, rhs: Self, borrow: bool) -> (Self, bool) {
             (c, b | d)
         }
 
+        /// Computes the absolute difference between `self` and `other`.
+        ///
+        /// # Examples
+        ///
+        /// Basic usage:
+        ///
+        /// ```
+        /// #![feature(int_abs_diff)]
+        #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".abs_diff(80), 20", stringify!($SelfT), ");")]
+        #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".abs_diff(110), 10", stringify!($SelfT), ");")]
+        /// ```
+        #[unstable(feature = "int_abs_diff", issue = "none")]
+        #[inline]
+        pub const fn abs_diff(self, other: Self) -> Self {
+            if self < other {
+                other - self
+            } else {
+                self - other
+            }
+        }
+
         /// Calculates the multiplication of `self` and `rhs`.
         ///
         /// Returns a tuple of the multiplication along with a boolean