Skip to main content

Integer

Trait Integer 

Source
pub trait Integer:
    Ring
    + Ord
    + Copy
    + Sized
    + BitAnd<Output = Self>
    + BitOr<Output = Self>
    + BitXor<Output = Self>
    + Not<Output = Self>
    + Shl<u32, Output = Self>
    + Shr<u32, Output = Self> {
    const MIN: Self;
    const MAX: Self;
    const BITS: u32;
Show 23 methods // Required methods fn count_ones(self) -> u32; fn count_zeros(self) -> u32; fn leading_zeros(self) -> u32; fn trailing_zeros(self) -> u32; fn swap_bytes(self) -> Self; fn from_be(x: Self) -> Self; fn from_le(x: Self) -> Self; fn to_be(self) -> Self; fn to_le(self) -> Self; fn checked_add(self, rhs: Self) -> Option<Self>; fn checked_sub(self, rhs: Self) -> Option<Self>; fn checked_mul(self, rhs: Self) -> Option<Self>; fn checked_div(self, rhs: Self) -> Option<Self>; fn checked_rem(self, rhs: Self) -> Option<Self>; fn saturating_add(self, rhs: Self) -> Self; fn saturating_sub(self, rhs: Self) -> Self; fn saturating_mul(self, rhs: Self) -> Self; fn wrapping_add(self, rhs: Self) -> Self; fn wrapping_sub(self, rhs: Self) -> Self; fn wrapping_mul(self, rhs: Self) -> Self; fn pow(self, exp: u32) -> Self; fn div_euclid(self, rhs: Self) -> Self; fn rem_euclid(self, rhs: Self) -> Self;
}
Expand description

A trait for primitive integer types.

This trait abstracts over both signed and unsigned integers, providing common operations and constants. It extends Ring from the algebraic hierarchy, reflecting that integers form a ring under addition and multiplication.

§Mathematical Background

The integers $\mathbb{Z}$ form a Euclidean Domain:

  • A commutative ring with identity
  • Division with remainder is well-defined: $a = bq + r$ where $0 \le r < |b|$
  • This enables the Euclidean algorithm for GCD

Fixed-width integers in computers are technically $\mathbb{Z}/2^n\mathbb{Z}$ (integers modulo $2^n$), but overflow behavior varies by build configuration.

Required Associated Constants§

Source

const MIN: Self

The minimum value representable by this type.

Source

const MAX: Self

The maximum value representable by this type.

Source

const BITS: u32

The size of this type in bits.

Required Methods§

Source

fn count_ones(self) -> u32

Returns the number of ones in the binary representation.

Source

fn count_zeros(self) -> u32

Returns the number of zeros in the binary representation.

Source

fn leading_zeros(self) -> u32

Returns the number of leading zeros in the binary representation.

Source

fn trailing_zeros(self) -> u32

Returns the number of trailing zeros in the binary representation.

Source

fn swap_bytes(self) -> Self

Reverses the byte order of the integer.

Source

fn from_be(x: Self) -> Self

Converts from big-endian to native byte order.

Source

fn from_le(x: Self) -> Self

Converts from little-endian to native byte order.

Source

fn to_be(self) -> Self

Converts to big-endian byte order.

Source

fn to_le(self) -> Self

Converts to little-endian byte order.

Source

fn checked_add(self, rhs: Self) -> Option<Self>

Checked addition. Returns None on overflow.

Source

fn checked_sub(self, rhs: Self) -> Option<Self>

Checked subtraction. Returns None on underflow.

Source

fn checked_mul(self, rhs: Self) -> Option<Self>

Checked multiplication. Returns None on overflow.

Source

fn checked_div(self, rhs: Self) -> Option<Self>

Checked division. Returns None if rhs == 0.

Source

fn checked_rem(self, rhs: Self) -> Option<Self>

Checked remainder. Returns None if rhs == 0.

Source

fn saturating_add(self, rhs: Self) -> Self

Saturating addition. Clamps at MAX or MIN.

Source

fn saturating_sub(self, rhs: Self) -> Self

Saturating subtraction. Clamps at MAX or MIN.

Source

fn saturating_mul(self, rhs: Self) -> Self

Saturating multiplication. Clamps at MAX or MIN.

Source

fn wrapping_add(self, rhs: Self) -> Self

Wrapping addition. Wraps around on overflow.

Source

fn wrapping_sub(self, rhs: Self) -> Self

Wrapping subtraction. Wraps around on underflow.

Source

fn wrapping_mul(self, rhs: Self) -> Self

Wrapping multiplication. Wraps around on overflow.

Source

fn pow(self, exp: u32) -> Self

Raises self to the power exp, using exponentiation by squaring.

Source

fn div_euclid(self, rhs: Self) -> Self

Calculates the quotient of Euclidean division.

Source

fn rem_euclid(self, rhs: Self) -> Self

Calculates the remainder of Euclidean division.

The result satisfies 0 <= r < |rhs| for all inputs.

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 Integer for i8

Source§

const MIN: Self = i8::MIN

Source§

const MAX: Self = i8::MAX

Source§

const BITS: u32 = i8::BITS

Source§

fn count_ones(self) -> u32

Source§

fn count_zeros(self) -> u32

Source§

fn leading_zeros(self) -> u32

Source§

fn trailing_zeros(self) -> u32

Source§

fn swap_bytes(self) -> Self

Source§

fn from_be(x: Self) -> Self

Source§

fn from_le(x: Self) -> Self

Source§

fn to_be(self) -> Self

Source§

fn to_le(self) -> Self

Source§

fn checked_add(self, rhs: Self) -> Option<Self>

Source§

fn checked_sub(self, rhs: Self) -> Option<Self>

Source§

fn checked_mul(self, rhs: Self) -> Option<Self>

Source§

fn checked_div(self, rhs: Self) -> Option<Self>

Source§

fn checked_rem(self, rhs: Self) -> Option<Self>

Source§

fn saturating_add(self, rhs: Self) -> Self

Source§

fn saturating_sub(self, rhs: Self) -> Self

Source§

fn saturating_mul(self, rhs: Self) -> Self

Source§

fn wrapping_add(self, rhs: Self) -> Self

Source§

fn wrapping_sub(self, rhs: Self) -> Self

Source§

fn wrapping_mul(self, rhs: Self) -> Self

Source§

fn pow(self, exp: u32) -> Self

Source§

fn div_euclid(self, rhs: Self) -> Self

Source§

fn rem_euclid(self, rhs: Self) -> Self

Source§

impl Integer for i16

Source§

const MIN: Self = i16::MIN

Source§

const MAX: Self = i16::MAX

Source§

const BITS: u32 = i16::BITS

Source§

fn count_ones(self) -> u32

Source§

fn count_zeros(self) -> u32

Source§

fn leading_zeros(self) -> u32

Source§

fn trailing_zeros(self) -> u32

Source§

fn swap_bytes(self) -> Self

Source§

fn from_be(x: Self) -> Self

Source§

fn from_le(x: Self) -> Self

Source§

fn to_be(self) -> Self

Source§

fn to_le(self) -> Self

Source§

fn checked_add(self, rhs: Self) -> Option<Self>

Source§

fn checked_sub(self, rhs: Self) -> Option<Self>

Source§

fn checked_mul(self, rhs: Self) -> Option<Self>

Source§

fn checked_div(self, rhs: Self) -> Option<Self>

Source§

fn checked_rem(self, rhs: Self) -> Option<Self>

Source§

fn saturating_add(self, rhs: Self) -> Self

Source§

fn saturating_sub(self, rhs: Self) -> Self

Source§

fn saturating_mul(self, rhs: Self) -> Self

Source§

fn wrapping_add(self, rhs: Self) -> Self

Source§

fn wrapping_sub(self, rhs: Self) -> Self

Source§

fn wrapping_mul(self, rhs: Self) -> Self

Source§

fn pow(self, exp: u32) -> Self

Source§

fn div_euclid(self, rhs: Self) -> Self

Source§

fn rem_euclid(self, rhs: Self) -> Self

Source§

impl Integer for i32

Source§

const MIN: Self = i32::MIN

Source§

const MAX: Self = i32::MAX

Source§

const BITS: u32 = i32::BITS

Source§

fn count_ones(self) -> u32

Source§

fn count_zeros(self) -> u32

Source§

fn leading_zeros(self) -> u32

Source§

fn trailing_zeros(self) -> u32

Source§

fn swap_bytes(self) -> Self

Source§

fn from_be(x: Self) -> Self

Source§

fn from_le(x: Self) -> Self

Source§

fn to_be(self) -> Self

Source§

fn to_le(self) -> Self

Source§

fn checked_add(self, rhs: Self) -> Option<Self>

Source§

fn checked_sub(self, rhs: Self) -> Option<Self>

Source§

fn checked_mul(self, rhs: Self) -> Option<Self>

Source§

fn checked_div(self, rhs: Self) -> Option<Self>

Source§

fn checked_rem(self, rhs: Self) -> Option<Self>

Source§

fn saturating_add(self, rhs: Self) -> Self

Source§

fn saturating_sub(self, rhs: Self) -> Self

Source§

fn saturating_mul(self, rhs: Self) -> Self

Source§

fn wrapping_add(self, rhs: Self) -> Self

Source§

fn wrapping_sub(self, rhs: Self) -> Self

Source§

fn wrapping_mul(self, rhs: Self) -> Self

Source§

fn pow(self, exp: u32) -> Self

Source§

fn div_euclid(self, rhs: Self) -> Self

Source§

fn rem_euclid(self, rhs: Self) -> Self

Source§

impl Integer for i64

Source§

const MIN: Self = i64::MIN

Source§

const MAX: Self = i64::MAX

Source§

const BITS: u32 = i64::BITS

Source§

fn count_ones(self) -> u32

Source§

fn count_zeros(self) -> u32

Source§

fn leading_zeros(self) -> u32

Source§

fn trailing_zeros(self) -> u32

Source§

fn swap_bytes(self) -> Self

Source§

fn from_be(x: Self) -> Self

Source§

fn from_le(x: Self) -> Self

Source§

fn to_be(self) -> Self

Source§

fn to_le(self) -> Self

Source§

fn checked_add(self, rhs: Self) -> Option<Self>

Source§

fn checked_sub(self, rhs: Self) -> Option<Self>

Source§

fn checked_mul(self, rhs: Self) -> Option<Self>

Source§

fn checked_div(self, rhs: Self) -> Option<Self>

Source§

fn checked_rem(self, rhs: Self) -> Option<Self>

Source§

fn saturating_add(self, rhs: Self) -> Self

Source§

fn saturating_sub(self, rhs: Self) -> Self

Source§

fn saturating_mul(self, rhs: Self) -> Self

Source§

fn wrapping_add(self, rhs: Self) -> Self

Source§

fn wrapping_sub(self, rhs: Self) -> Self

Source§

fn wrapping_mul(self, rhs: Self) -> Self

Source§

fn pow(self, exp: u32) -> Self

Source§

fn div_euclid(self, rhs: Self) -> Self

Source§

fn rem_euclid(self, rhs: Self) -> Self

Source§

impl Integer for i128

Source§

const MIN: Self = i128::MIN

Source§

const MAX: Self = i128::MAX

Source§

const BITS: u32 = i128::BITS

Source§

fn count_ones(self) -> u32

Source§

fn count_zeros(self) -> u32

Source§

fn leading_zeros(self) -> u32

Source§

fn trailing_zeros(self) -> u32

Source§

fn swap_bytes(self) -> Self

Source§

fn from_be(x: Self) -> Self

Source§

fn from_le(x: Self) -> Self

Source§

fn to_be(self) -> Self

Source§

fn to_le(self) -> Self

Source§

fn checked_add(self, rhs: Self) -> Option<Self>

Source§

fn checked_sub(self, rhs: Self) -> Option<Self>

Source§

fn checked_mul(self, rhs: Self) -> Option<Self>

Source§

fn checked_div(self, rhs: Self) -> Option<Self>

Source§

fn checked_rem(self, rhs: Self) -> Option<Self>

Source§

fn saturating_add(self, rhs: Self) -> Self

Source§

fn saturating_sub(self, rhs: Self) -> Self

Source§

fn saturating_mul(self, rhs: Self) -> Self

Source§

fn wrapping_add(self, rhs: Self) -> Self

Source§

fn wrapping_sub(self, rhs: Self) -> Self

Source§

fn wrapping_mul(self, rhs: Self) -> Self

Source§

fn pow(self, exp: u32) -> Self

Source§

fn div_euclid(self, rhs: Self) -> Self

Source§

fn rem_euclid(self, rhs: Self) -> Self

Source§

impl Integer for isize

Source§

const MIN: Self = isize::MIN

Source§

const MAX: Self = isize::MAX

Source§

const BITS: u32 = isize::BITS

Source§

fn count_ones(self) -> u32

Source§

fn count_zeros(self) -> u32

Source§

fn leading_zeros(self) -> u32

Source§

fn trailing_zeros(self) -> u32

Source§

fn swap_bytes(self) -> Self

Source§

fn from_be(x: Self) -> Self

Source§

fn from_le(x: Self) -> Self

Source§

fn to_be(self) -> Self

Source§

fn to_le(self) -> Self

Source§

fn checked_add(self, rhs: Self) -> Option<Self>

Source§

fn checked_sub(self, rhs: Self) -> Option<Self>

Source§

fn checked_mul(self, rhs: Self) -> Option<Self>

Source§

fn checked_div(self, rhs: Self) -> Option<Self>

Source§

fn checked_rem(self, rhs: Self) -> Option<Self>

Source§

fn saturating_add(self, rhs: Self) -> Self

Source§

fn saturating_sub(self, rhs: Self) -> Self

Source§

fn saturating_mul(self, rhs: Self) -> Self

Source§

fn wrapping_add(self, rhs: Self) -> Self

Source§

fn wrapping_sub(self, rhs: Self) -> Self

Source§

fn wrapping_mul(self, rhs: Self) -> Self

Source§

fn pow(self, exp: u32) -> Self

Source§

fn div_euclid(self, rhs: Self) -> Self

Source§

fn rem_euclid(self, rhs: Self) -> Self

Source§

impl Integer for u8

Source§

const MIN: Self = u8::MIN

Source§

const MAX: Self = u8::MAX

Source§

const BITS: u32 = u8::BITS

Source§

fn count_ones(self) -> u32

Source§

fn count_zeros(self) -> u32

Source§

fn leading_zeros(self) -> u32

Source§

fn trailing_zeros(self) -> u32

Source§

fn swap_bytes(self) -> Self

Source§

fn from_be(x: Self) -> Self

Source§

fn from_le(x: Self) -> Self

Source§

fn to_be(self) -> Self

Source§

fn to_le(self) -> Self

Source§

fn checked_add(self, rhs: Self) -> Option<Self>

Source§

fn checked_sub(self, rhs: Self) -> Option<Self>

Source§

fn checked_mul(self, rhs: Self) -> Option<Self>

Source§

fn checked_div(self, rhs: Self) -> Option<Self>

Source§

fn checked_rem(self, rhs: Self) -> Option<Self>

Source§

fn saturating_add(self, rhs: Self) -> Self

Source§

fn saturating_sub(self, rhs: Self) -> Self

Source§

fn saturating_mul(self, rhs: Self) -> Self

Source§

fn wrapping_add(self, rhs: Self) -> Self

Source§

fn wrapping_sub(self, rhs: Self) -> Self

Source§

fn wrapping_mul(self, rhs: Self) -> Self

Source§

fn pow(self, exp: u32) -> Self

Source§

fn div_euclid(self, rhs: Self) -> Self

Source§

fn rem_euclid(self, rhs: Self) -> Self

Source§

impl Integer for u16

Source§

const MIN: Self = u16::MIN

Source§

const MAX: Self = u16::MAX

Source§

const BITS: u32 = u16::BITS

Source§

fn count_ones(self) -> u32

Source§

fn count_zeros(self) -> u32

Source§

fn leading_zeros(self) -> u32

Source§

fn trailing_zeros(self) -> u32

Source§

fn swap_bytes(self) -> Self

Source§

fn from_be(x: Self) -> Self

Source§

fn from_le(x: Self) -> Self

Source§

fn to_be(self) -> Self

Source§

fn to_le(self) -> Self

Source§

fn checked_add(self, rhs: Self) -> Option<Self>

Source§

fn checked_sub(self, rhs: Self) -> Option<Self>

Source§

fn checked_mul(self, rhs: Self) -> Option<Self>

Source§

fn checked_div(self, rhs: Self) -> Option<Self>

Source§

fn checked_rem(self, rhs: Self) -> Option<Self>

Source§

fn saturating_add(self, rhs: Self) -> Self

Source§

fn saturating_sub(self, rhs: Self) -> Self

Source§

fn saturating_mul(self, rhs: Self) -> Self

Source§

fn wrapping_add(self, rhs: Self) -> Self

Source§

fn wrapping_sub(self, rhs: Self) -> Self

Source§

fn wrapping_mul(self, rhs: Self) -> Self

Source§

fn pow(self, exp: u32) -> Self

Source§

fn div_euclid(self, rhs: Self) -> Self

Source§

fn rem_euclid(self, rhs: Self) -> Self

Source§

impl Integer for u32

Source§

const MIN: Self = u32::MIN

Source§

const MAX: Self = u32::MAX

Source§

const BITS: u32 = u32::BITS

Source§

fn count_ones(self) -> u32

Source§

fn count_zeros(self) -> u32

Source§

fn leading_zeros(self) -> u32

Source§

fn trailing_zeros(self) -> u32

Source§

fn swap_bytes(self) -> Self

Source§

fn from_be(x: Self) -> Self

Source§

fn from_le(x: Self) -> Self

Source§

fn to_be(self) -> Self

Source§

fn to_le(self) -> Self

Source§

fn checked_add(self, rhs: Self) -> Option<Self>

Source§

fn checked_sub(self, rhs: Self) -> Option<Self>

Source§

fn checked_mul(self, rhs: Self) -> Option<Self>

Source§

fn checked_div(self, rhs: Self) -> Option<Self>

Source§

fn checked_rem(self, rhs: Self) -> Option<Self>

Source§

fn saturating_add(self, rhs: Self) -> Self

Source§

fn saturating_sub(self, rhs: Self) -> Self

Source§

fn saturating_mul(self, rhs: Self) -> Self

Source§

fn wrapping_add(self, rhs: Self) -> Self

Source§

fn wrapping_sub(self, rhs: Self) -> Self

Source§

fn wrapping_mul(self, rhs: Self) -> Self

Source§

fn pow(self, exp: u32) -> Self

Source§

fn div_euclid(self, rhs: Self) -> Self

Source§

fn rem_euclid(self, rhs: Self) -> Self

Source§

impl Integer for u64

Source§

const MIN: Self = u64::MIN

Source§

const MAX: Self = u64::MAX

Source§

const BITS: u32 = u64::BITS

Source§

fn count_ones(self) -> u32

Source§

fn count_zeros(self) -> u32

Source§

fn leading_zeros(self) -> u32

Source§

fn trailing_zeros(self) -> u32

Source§

fn swap_bytes(self) -> Self

Source§

fn from_be(x: Self) -> Self

Source§

fn from_le(x: Self) -> Self

Source§

fn to_be(self) -> Self

Source§

fn to_le(self) -> Self

Source§

fn checked_add(self, rhs: Self) -> Option<Self>

Source§

fn checked_sub(self, rhs: Self) -> Option<Self>

Source§

fn checked_mul(self, rhs: Self) -> Option<Self>

Source§

fn checked_div(self, rhs: Self) -> Option<Self>

Source§

fn checked_rem(self, rhs: Self) -> Option<Self>

Source§

fn saturating_add(self, rhs: Self) -> Self

Source§

fn saturating_sub(self, rhs: Self) -> Self

Source§

fn saturating_mul(self, rhs: Self) -> Self

Source§

fn wrapping_add(self, rhs: Self) -> Self

Source§

fn wrapping_sub(self, rhs: Self) -> Self

Source§

fn wrapping_mul(self, rhs: Self) -> Self

Source§

fn pow(self, exp: u32) -> Self

Source§

fn div_euclid(self, rhs: Self) -> Self

Source§

fn rem_euclid(self, rhs: Self) -> Self

Source§

impl Integer for u128

Source§

const MIN: Self = u128::MIN

Source§

const MAX: Self = u128::MAX

Source§

const BITS: u32 = u128::BITS

Source§

fn count_ones(self) -> u32

Source§

fn count_zeros(self) -> u32

Source§

fn leading_zeros(self) -> u32

Source§

fn trailing_zeros(self) -> u32

Source§

fn swap_bytes(self) -> Self

Source§

fn from_be(x: Self) -> Self

Source§

fn from_le(x: Self) -> Self

Source§

fn to_be(self) -> Self

Source§

fn to_le(self) -> Self

Source§

fn checked_add(self, rhs: Self) -> Option<Self>

Source§

fn checked_sub(self, rhs: Self) -> Option<Self>

Source§

fn checked_mul(self, rhs: Self) -> Option<Self>

Source§

fn checked_div(self, rhs: Self) -> Option<Self>

Source§

fn checked_rem(self, rhs: Self) -> Option<Self>

Source§

fn saturating_add(self, rhs: Self) -> Self

Source§

fn saturating_sub(self, rhs: Self) -> Self

Source§

fn saturating_mul(self, rhs: Self) -> Self

Source§

fn wrapping_add(self, rhs: Self) -> Self

Source§

fn wrapping_sub(self, rhs: Self) -> Self

Source§

fn wrapping_mul(self, rhs: Self) -> Self

Source§

fn pow(self, exp: u32) -> Self

Source§

fn div_euclid(self, rhs: Self) -> Self

Source§

fn rem_euclid(self, rhs: Self) -> Self

Source§

impl Integer for usize

Source§

const MIN: Self = usize::MIN

Source§

const MAX: Self = usize::MAX

Source§

const BITS: u32 = usize::BITS

Source§

fn count_ones(self) -> u32

Source§

fn count_zeros(self) -> u32

Source§

fn leading_zeros(self) -> u32

Source§

fn trailing_zeros(self) -> u32

Source§

fn swap_bytes(self) -> Self

Source§

fn from_be(x: Self) -> Self

Source§

fn from_le(x: Self) -> Self

Source§

fn to_be(self) -> Self

Source§

fn to_le(self) -> Self

Source§

fn checked_add(self, rhs: Self) -> Option<Self>

Source§

fn checked_sub(self, rhs: Self) -> Option<Self>

Source§

fn checked_mul(self, rhs: Self) -> Option<Self>

Source§

fn checked_div(self, rhs: Self) -> Option<Self>

Source§

fn checked_rem(self, rhs: Self) -> Option<Self>

Source§

fn saturating_add(self, rhs: Self) -> Self

Source§

fn saturating_sub(self, rhs: Self) -> Self

Source§

fn saturating_mul(self, rhs: Self) -> Self

Source§

fn wrapping_add(self, rhs: Self) -> Self

Source§

fn wrapping_sub(self, rhs: Self) -> Self

Source§

fn wrapping_mul(self, rhs: Self) -> Self

Source§

fn pow(self, exp: u32) -> Self

Source§

fn div_euclid(self, rhs: Self) -> Self

Source§

fn rem_euclid(self, rhs: Self) -> Self

Implementors§