pub trait CheckedNeg: Sized {
type Output;
// Required method
fn checked_neg(self) -> Option<Self::Output>;
}Expand description
Performs negation, returning None if the result can’t be represented.
Required Associated Types§
Required Methods§
Sourcefn checked_neg(self) -> Option<Self::Output>
fn checked_neg(self) -> Option<Self::Output>
Negates a number, returning None for results that can’t be represented, like signed MIN
values that can’t be positive, or non-zero unsigned values that can’t be negative.
§Examples
use const_num_traits::CheckedNeg;
use std::i32::MIN;
assert_eq!(CheckedNeg::checked_neg(1_i32), Some(-1));
assert_eq!(CheckedNeg::checked_neg(-1_i32), Some(1));
assert_eq!(CheckedNeg::checked_neg(MIN), None);
assert_eq!(CheckedNeg::checked_neg(0_u32), Some(0));
assert_eq!(CheckedNeg::checked_neg(1_u32), None);Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".