Trait std::num::IntStable
[-] [+]
[src]
pub trait Int: NumCast + Add<Self> + Sub<Self> + Mul<Self> + Div<Self> + Rem<Self> + Not + BitAnd<Self> + BitOr<Self> + BitXor<Self> + Shl<usize> + Shr<usize> + PartialEq<Self> + Eq + Ord + PartialOrd<Self> + Clone where Self: Copy, Self: NumCast, Self: Add<Self>, Self: Sub<Self>, Self: Mul<Self>, Self: Div<Self>, Self: Rem<Self>, Self: Not, Self: BitAnd<Self>, Self: BitOr<Self>, Self: BitXor<Self>, Self: Shl<usize>, Self: Shr<usize>, Self: PartialEq<Self>, Self: Eq, Self: Ord, Self: PartialOrd<Self>, Self: Clone, <Self as Add<Self>>::Output == Self, <Self as Sub<Self>>::Output == Self, <Self as Mul<Self>>::Output == Self, <Self as Div<Self>>::Output == Self, <Self as Rem<Self>>::Output == Self, <Self as Not>::Output == Self, <Self as BitAnd<Self>>::Output == Self, <Self as BitOr<Self>>::Output == Self, <Self as BitXor<Self>>::Output == Self, <Self as Shl<usize>>::Output == Self, <Self as Shr<usize>>::Output == Self {
fn zero() -> Self;
fn one() -> Self;
fn min_value() -> Self;
fn max_value() -> Self;
fn count_ones(self) -> usize;
fn leading_zeros(self) -> usize;
fn trailing_zeros(self) -> usize;
fn rotate_left(self, n: usize) -> Self;
fn rotate_right(self, n: usize) -> Self;
fn swap_bytes(self) -> Self;
fn checked_add(self, other: Self) -> Option<Self>;
fn checked_sub(self, other: Self) -> Option<Self>;
fn checked_mul(self, other: Self) -> Option<Self>;
fn checked_div(self, other: Self) -> Option<Self>;
fn count_zeros(self) -> usize { ... }
fn from_be(x: Self) -> Self { ... }
fn from_le(x: Self) -> Self { ... }
fn to_be(self) -> Self { ... }
fn to_le(self) -> Self { ... }
fn saturating_add(self, other: Self) -> Self { ... }
fn saturating_sub(self, other: Self) -> Self { ... }
fn pow(self, exp: usize) -> Self { ... }
}
A built-in signed or unsigned integer.
Required Methods
fn zero() -> Self
Returns the 0
value of this integer type.
fn one() -> Self
Returns the 1
value of this integer type.
fn min_value() -> Self
Returns the smallest value that can be represented by this integer type.
fn max_value() -> Self
Returns the largest value that can be represented by this integer type.
fn count_ones(self) -> usize
Returns the number of ones in the binary representation of self
.
Example
fn main() { use std::num::Int; let n = 0b01001100u8; assert_eq!(n.count_ones(), 3); }use std::num::Int; let n = 0b01001100u8; assert_eq!(n.count_ones(), 3);
fn leading_zeros(self) -> usize
Returns the number of leading zeros in the binary representation
of self
.
Example
fn main() { use std::num::Int; let n = 0b0101000u16; assert_eq!(n.leading_zeros(), 10); }use std::num::Int; let n = 0b0101000u16; assert_eq!(n.leading_zeros(), 10);
fn trailing_zeros(self) -> usize
Returns the number of trailing zeros in the binary representation
of self
.
Example
fn main() { use std::num::Int; let n = 0b0101000u16; assert_eq!(n.trailing_zeros(), 3); }use std::num::Int; let n = 0b0101000u16; assert_eq!(n.trailing_zeros(), 3);
fn rotate_left(self, n: usize) -> Self
Shifts the bits to the left by a specified amount amount, n
, wrapping
the truncated bits to the end of the resulting integer.
Example
fn main() { use std::num::Int; let n = 0x0123456789ABCDEFu64; let m = 0x3456789ABCDEF012u64; assert_eq!(n.rotate_left(12), m); }use std::num::Int; let n = 0x0123456789ABCDEFu64; let m = 0x3456789ABCDEF012u64; assert_eq!(n.rotate_left(12), m);
fn rotate_right(self, n: usize) -> Self
Shifts the bits to the right by a specified amount amount, n
, wrapping
the truncated bits to the beginning of the resulting integer.
Example
fn main() { use std::num::Int; let n = 0x0123456789ABCDEFu64; let m = 0xDEF0123456789ABCu64; assert_eq!(n.rotate_right(12), m); }use std::num::Int; let n = 0x0123456789ABCDEFu64; let m = 0xDEF0123456789ABCu64; assert_eq!(n.rotate_right(12), m);
fn swap_bytes(self) -> Self
Reverses the byte order of the integer.
Example
fn main() { use std::num::Int; let n = 0x0123456789ABCDEFu64; let m = 0xEFCDAB8967452301u64; assert_eq!(n.swap_bytes(), m); }use std::num::Int; let n = 0x0123456789ABCDEFu64; let m = 0xEFCDAB8967452301u64; assert_eq!(n.swap_bytes(), m);
fn checked_add(self, other: Self) -> Option<Self>
Checked integer addition. Computes self + other
, returning None
if
overflow occurred.
Example
fn main() { use std::num::Int; assert_eq!(5u16.checked_add(65530), Some(65535)); assert_eq!(6u16.checked_add(65530), None); }use std::num::Int; assert_eq!(5u16.checked_add(65530), Some(65535)); assert_eq!(6u16.checked_add(65530), None);
fn checked_sub(self, other: Self) -> Option<Self>
Checked integer subtraction. Computes self - other
, returning None
if underflow occurred.
Example
fn main() { use std::num::Int; assert_eq!((-127i8).checked_sub(1), Some(-128)); assert_eq!((-128i8).checked_sub(1), None); }use std::num::Int; assert_eq!((-127i8).checked_sub(1), Some(-128)); assert_eq!((-128i8).checked_sub(1), None);
fn checked_mul(self, other: Self) -> Option<Self>
Checked integer multiplication. Computes self * other
, returning
None
if underflow or overflow occurred.
Example
fn main() { use std::num::Int; assert_eq!(5u8.checked_mul(51), Some(255)); assert_eq!(5u8.checked_mul(52), None); }use std::num::Int; assert_eq!(5u8.checked_mul(51), Some(255)); assert_eq!(5u8.checked_mul(52), None);
fn checked_div(self, other: Self) -> Option<Self>
Checked integer division. Computes self / other
, returning None
if
other == 0
or the operation results in underflow or overflow.
Example
fn main() { use std::num::Int; assert_eq!((-127i8).checked_div(-1), Some(127)); assert_eq!((-128i8).checked_div(-1), None); assert_eq!((1i8).checked_div(0), None); }use std::num::Int; assert_eq!((-127i8).checked_div(-1), Some(127)); assert_eq!((-128i8).checked_div(-1), None); assert_eq!((1i8).checked_div(0), None);
Provided Methods
fn count_zeros(self) -> usize
Returns the number of zeros in the binary representation of self
.
Example
fn main() { use std::num::Int; let n = 0b01001100u8; assert_eq!(n.count_zeros(), 5); }use std::num::Int; let n = 0b01001100u8; assert_eq!(n.count_zeros(), 5);
fn from_be(x: Self) -> Self
Convert an integer from big endian to the target's endianness.
On big endian this is a no-op. On little endian the bytes are swapped.
Example
fn main() { use std::num::Int; let n = 0x0123456789ABCDEFu64; if cfg!(target_endian = "big") { assert_eq!(Int::from_be(n), n) } else { assert_eq!(Int::from_be(n), n.swap_bytes()) } }use std::num::Int; let n = 0x0123456789ABCDEFu64; if cfg!(target_endian = "big") { assert_eq!(Int::from_be(n), n) } else { assert_eq!(Int::from_be(n), n.swap_bytes()) }
fn from_le(x: Self) -> Self
Convert an integer from little endian to the target's endianness.
On little endian this is a no-op. On big endian the bytes are swapped.
Example
fn main() { use std::num::Int; let n = 0x0123456789ABCDEFu64; if cfg!(target_endian = "little") { assert_eq!(Int::from_le(n), n) } else { assert_eq!(Int::from_le(n), n.swap_bytes()) } }use std::num::Int; let n = 0x0123456789ABCDEFu64; if cfg!(target_endian = "little") { assert_eq!(Int::from_le(n), n) } else { assert_eq!(Int::from_le(n), n.swap_bytes()) }
fn to_be(self) -> Self
Convert self
to big endian from the target's endianness.
On big endian this is a no-op. On little endian the bytes are swapped.
Example
fn main() { use std::num::Int; let n = 0x0123456789ABCDEFu64; if cfg!(target_endian = "big") { assert_eq!(n.to_be(), n) } else { assert_eq!(n.to_be(), n.swap_bytes()) } }use std::num::Int; let n = 0x0123456789ABCDEFu64; if cfg!(target_endian = "big") { assert_eq!(n.to_be(), n) } else { assert_eq!(n.to_be(), n.swap_bytes()) }
fn to_le(self) -> Self
Convert self
to little endian from the target's endianness.
On little endian this is a no-op. On big endian the bytes are swapped.
Example
fn main() { use std::num::Int; let n = 0x0123456789ABCDEFu64; if cfg!(target_endian = "little") { assert_eq!(n.to_le(), n) } else { assert_eq!(n.to_le(), n.swap_bytes()) } }use std::num::Int; let n = 0x0123456789ABCDEFu64; if cfg!(target_endian = "little") { assert_eq!(n.to_le(), n) } else { assert_eq!(n.to_le(), n.swap_bytes()) }
fn saturating_add(self, other: Self) -> Self
Saturating integer addition. Computes self + other
, saturating at
the numeric bounds instead of overflowing.
fn saturating_sub(self, other: Self) -> Self
Saturating integer subtraction. Computes self - other
, saturating at
the numeric bounds instead of overflowing.
fn pow(self, exp: usize) -> Self
Raises self to the power of exp
, using exponentiation by squaring.
Example
fn main() { use std::num::Int; assert_eq!(2.pow(4), 16); }use std::num::Int; assert_eq!(2.pow(4), 16);