pub trait SignedInt: Integer + Neg<Output = Self> {
// Required methods
fn abs(self) -> Self;
fn signum(self) -> Self;
fn is_negative(self) -> bool;
fn is_positive(self) -> bool;
fn checked_abs(self) -> Option<Self>;
fn checked_neg(self) -> Option<Self>;
fn saturating_abs(self) -> Self;
fn wrapping_abs(self) -> Self;
fn wrapping_neg(self) -> Self;
}Expand description
A trait for signed integer types.
Extends Integer with operations specific to signed types, such as
absolute value, signum, and negation.
§Mathematical Background
Signed integers represent $\mathbb{Z}$ (the integers) within a bounded range. They support the additive inverse operation ($-a$), forming a true additive group (unlike unsigned integers in standard arithmetic).
Required Methods§
Sourcefn abs(self) -> Self
fn abs(self) -> Self
Returns the absolute value of the integer.
§Overflow
For MIN values (e.g., i8::MIN = -128), the absolute value cannot
be represented and will panic in debug mode or wrap in release.
Use checked_abs for safe handling.
Sourcefn signum(self) -> Self
fn signum(self) -> Self
Returns the sign of the integer.
-1if negative0if zero1if positive
Sourcefn is_negative(self) -> bool
fn is_negative(self) -> bool
Returns true if the integer is negative.
Sourcefn is_positive(self) -> bool
fn is_positive(self) -> bool
Returns true if the integer is positive.
Sourcefn checked_abs(self) -> Option<Self>
fn checked_abs(self) -> Option<Self>
Checked absolute value. Returns None for MIN.
Sourcefn checked_neg(self) -> Option<Self>
fn checked_neg(self) -> Option<Self>
Checked negation. Returns None for MIN.
Sourcefn saturating_abs(self) -> Self
fn saturating_abs(self) -> Self
Saturating absolute value. Returns MAX for MIN.
Sourcefn wrapping_abs(self) -> Self
fn wrapping_abs(self) -> Self
Wrapping absolute value. Wraps MIN to MIN.
Sourcefn wrapping_neg(self) -> Self
fn wrapping_neg(self) -> Self
Wrapping negation.
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.