pub trait WrappingNeg: Sized {
// Required method
fn wrapping_neg(&self) -> Self;
}
Expand description
Performs a negation that does not panic.
Required Methods§
Sourcefn wrapping_neg(&self) -> Self
fn wrapping_neg(&self) -> Self
Wrapping (modular) negation. Computes -self
,
wrapping around at the boundary of the type.
Since unsigned types do not have negative equivalents
all applications of this function will wrap (except for -0
).
For values smaller than the corresponding signed type’s maximum
the result is the same as casting the corresponding signed value.
Any larger values are equivalent to MAX + 1 - (val - MAX - 1)
where
MAX
is the corresponding signed type’s maximum.
use num_traits::WrappingNeg;
assert_eq!(100i8.wrapping_neg(), -100);
assert_eq!((-100i8).wrapping_neg(), 100);
assert_eq!((-128i8).wrapping_neg(), -128); // wrapped!
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.