Skip to main content

SignedInt

Trait SignedInt 

Source
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§

Source

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.

Source

fn signum(self) -> Self

Returns the sign of the integer.

  • -1 if negative
  • 0 if zero
  • 1 if positive
Source

fn is_negative(self) -> bool

Returns true if the integer is negative.

Source

fn is_positive(self) -> bool

Returns true if the integer is positive.

Source

fn checked_abs(self) -> Option<Self>

Checked absolute value. Returns None for MIN.

Source

fn checked_neg(self) -> Option<Self>

Checked negation. Returns None for MIN.

Source

fn saturating_abs(self) -> Self

Saturating absolute value. Returns MAX for MIN.

Source

fn wrapping_abs(self) -> Self

Wrapping absolute value. Wraps MIN to MIN.

Source

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.

Implementations on Foreign Types§

Source§

impl SignedInt for i8

Source§

fn abs(self) -> Self

Source§

fn signum(self) -> Self

Source§

fn is_negative(self) -> bool

Source§

fn is_positive(self) -> bool

Source§

fn checked_abs(self) -> Option<Self>

Source§

fn checked_neg(self) -> Option<Self>

Source§

fn saturating_abs(self) -> Self

Source§

fn wrapping_abs(self) -> Self

Source§

fn wrapping_neg(self) -> Self

Source§

impl SignedInt for i16

Source§

fn abs(self) -> Self

Source§

fn signum(self) -> Self

Source§

fn is_negative(self) -> bool

Source§

fn is_positive(self) -> bool

Source§

fn checked_abs(self) -> Option<Self>

Source§

fn checked_neg(self) -> Option<Self>

Source§

fn saturating_abs(self) -> Self

Source§

fn wrapping_abs(self) -> Self

Source§

fn wrapping_neg(self) -> Self

Source§

impl SignedInt for i32

Source§

fn abs(self) -> Self

Source§

fn signum(self) -> Self

Source§

fn is_negative(self) -> bool

Source§

fn is_positive(self) -> bool

Source§

fn checked_abs(self) -> Option<Self>

Source§

fn checked_neg(self) -> Option<Self>

Source§

fn saturating_abs(self) -> Self

Source§

fn wrapping_abs(self) -> Self

Source§

fn wrapping_neg(self) -> Self

Source§

impl SignedInt for i64

Source§

fn abs(self) -> Self

Source§

fn signum(self) -> Self

Source§

fn is_negative(self) -> bool

Source§

fn is_positive(self) -> bool

Source§

fn checked_abs(self) -> Option<Self>

Source§

fn checked_neg(self) -> Option<Self>

Source§

fn saturating_abs(self) -> Self

Source§

fn wrapping_abs(self) -> Self

Source§

fn wrapping_neg(self) -> Self

Source§

impl SignedInt for i128

Source§

fn abs(self) -> Self

Source§

fn signum(self) -> Self

Source§

fn is_negative(self) -> bool

Source§

fn is_positive(self) -> bool

Source§

fn checked_abs(self) -> Option<Self>

Source§

fn checked_neg(self) -> Option<Self>

Source§

fn saturating_abs(self) -> Self

Source§

fn wrapping_abs(self) -> Self

Source§

fn wrapping_neg(self) -> Self

Source§

impl SignedInt for isize

Source§

fn abs(self) -> Self

Source§

fn signum(self) -> Self

Source§

fn is_negative(self) -> bool

Source§

fn is_positive(self) -> bool

Source§

fn checked_abs(self) -> Option<Self>

Source§

fn checked_neg(self) -> Option<Self>

Source§

fn saturating_abs(self) -> Self

Source§

fn wrapping_abs(self) -> Self

Source§

fn wrapping_neg(self) -> Self

Implementors§