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§
Required Methods§
Sourcefn count_ones(self) -> u32
fn count_ones(self) -> u32
Returns the number of ones in the binary representation.
Sourcefn count_zeros(self) -> u32
fn count_zeros(self) -> u32
Returns the number of zeros in the binary representation.
Sourcefn leading_zeros(self) -> u32
fn leading_zeros(self) -> u32
Returns the number of leading zeros in the binary representation.
Sourcefn trailing_zeros(self) -> u32
fn trailing_zeros(self) -> u32
Returns the number of trailing zeros in the binary representation.
Sourcefn swap_bytes(self) -> Self
fn swap_bytes(self) -> Self
Reverses the byte order of the integer.
Sourcefn checked_add(self, rhs: Self) -> Option<Self>
fn checked_add(self, rhs: Self) -> Option<Self>
Checked addition. Returns None on overflow.
Sourcefn checked_sub(self, rhs: Self) -> Option<Self>
fn checked_sub(self, rhs: Self) -> Option<Self>
Checked subtraction. Returns None on underflow.
Sourcefn checked_mul(self, rhs: Self) -> Option<Self>
fn checked_mul(self, rhs: Self) -> Option<Self>
Checked multiplication. Returns None on overflow.
Sourcefn checked_div(self, rhs: Self) -> Option<Self>
fn checked_div(self, rhs: Self) -> Option<Self>
Checked division. Returns None if rhs == 0.
Sourcefn checked_rem(self, rhs: Self) -> Option<Self>
fn checked_rem(self, rhs: Self) -> Option<Self>
Checked remainder. Returns None if rhs == 0.
Sourcefn saturating_add(self, rhs: Self) -> Self
fn saturating_add(self, rhs: Self) -> Self
Saturating addition. Clamps at MAX or MIN.
Sourcefn saturating_sub(self, rhs: Self) -> Self
fn saturating_sub(self, rhs: Self) -> Self
Saturating subtraction. Clamps at MAX or MIN.
Sourcefn saturating_mul(self, rhs: Self) -> Self
fn saturating_mul(self, rhs: Self) -> Self
Saturating multiplication. Clamps at MAX or MIN.
Sourcefn wrapping_add(self, rhs: Self) -> Self
fn wrapping_add(self, rhs: Self) -> Self
Wrapping addition. Wraps around on overflow.
Sourcefn wrapping_sub(self, rhs: Self) -> Self
fn wrapping_sub(self, rhs: Self) -> Self
Wrapping subtraction. Wraps around on underflow.
Sourcefn wrapping_mul(self, rhs: Self) -> Self
fn wrapping_mul(self, rhs: Self) -> Self
Wrapping multiplication. Wraps around on overflow.
Sourcefn div_euclid(self, rhs: Self) -> Self
fn div_euclid(self, rhs: Self) -> Self
Calculates the quotient of Euclidean division.
Sourcefn rem_euclid(self, rhs: Self) -> Self
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.