Skip to content

Commit 86d85b6

Browse files
authored
Rollup merge of rust-lang#74759 - carbotaniuman:uabs, r=shepmaster
add `unsigned_abs` to signed integers Mentioned on rust-lang/rfcs#2914 This PR simply adds an `unsigned_abs` to signed integers function which returns the correct absolute value as a unsigned integer.
2 parents c186aed + 784dd22 commit 86d85b6

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

library/core/src/num/mod.rs

+23
Original file line numberDiff line numberDiff line change
@@ -1601,6 +1601,29 @@ $EndFeature, "
16011601
}
16021602
}
16031603

1604+
doc_comment! {
1605+
concat!("Computes the absolute value of `self` without any wrapping
1606+
or panicking.
1607+
1608+
1609+
# Examples
1610+
1611+
Basic usage:
1612+
1613+
```
1614+
", $Feature, "#![feature(unsigned_abs)]
1615+
assert_eq!(100", stringify!($SelfT), ".unsigned_abs(), 100", stringify!($UnsignedT), ");
1616+
assert_eq!((-100", stringify!($SelfT), ").unsigned_abs(), 100", stringify!($UnsignedT), ");
1617+
assert_eq!((-128i8).unsigned_abs(), 128u8);",
1618+
$EndFeature, "
1619+
```"),
1620+
#[unstable(feature = "unsigned_abs", issue = "74913")]
1621+
#[inline]
1622+
pub const fn unsigned_abs(self) -> $UnsignedT {
1623+
self.wrapping_abs() as $UnsignedT
1624+
}
1625+
}
1626+
16041627
doc_comment! {
16051628
concat!("Wrapping (modular) exponentiation. Computes `self.pow(exp)`,
16061629
wrapping around at the boundary of the type.

0 commit comments

Comments
 (0)