Struct ramp::int::Int [] [src]

pub struct Int {
    // some fields omitted
}

An arbitrary-precision signed integer.

This type grows to the size it needs to in order to store the result of any operation.

Creation

An Int can be constructed in a number of ways:

Output

Int supports all the formatting traits, allowing it to be used just like a regular integer when used in format! and similar macros. Int also supports conversion to primitive integer types, truncating if the Int cannot fit into the target type. Conversion to primtive integers is done with the From trait:

  let big_i   = Int::from(123456789);
  let i = i32::from(&big_i);
  assert_eq!(123456789, i);

Usage

Int has a number of operator overloads to make working with them as painless as possible.

The most basic usage is simply a + b or similar. Assuming a and b are of type Int, this operation will consume both operands, reusing the storage from one of them. If you do not wish your operands to be moved, one or both of them can be references: &a + &b works as well, but requires an entire new Int to be allocated for the return value.

There are also a overloads for a small number of primitive integer types, namely i32 and usize. While automatic type widening isn't done in Rust in general, many operations are much more efficient when working with a single integer. This means you can do a + 1 knowing that it will be performed as efficiently as possible. Comparison with these integer types is also possible, allowing checks for small constant values to be done easily:

  let big_i   = Int::from(123456789);
  assert!(big_i == 123456789);

Semantics

Addition, subtraction and multiplication follow the expected rules for integers. Division of two integers, N / D is defined as producing two values: a quotient, Q, and a remainder, R, such that the following equation holds: N = Q*D + R. The division operator itself returns Q while the remainder/modulo operator returns R.

The "bit-shift" operations are defined as being multiplication and division by a power-of-two for shift-left and shift-right respectively. The sign of the number is unaffected.

The remaining bitwise operands act as if the numbers are stored in two's complement format and as if the two inputs have the same number of bits.

Methods

impl Int

fn zero() -> Int

fn one() -> Int

fn from_single_limb(limb: Limb) -> Int

Creates a new Int from the given Limb.

fn sign(&self) -> i32

Returns the sign of the Int as either -1, 0 or 1 for self being negative, zero or positive, respectively.

fn abs(self) -> Int

Consumes self and returns the absolute value

fn to_single_limb(&self) -> Limb

Returns the least-significant limb of self.

fn abs_cmp(&self, other: &Int) -> Ordering

Compare the absolute value of self to the absolute value of other, returning an Ordering with the result.

fn shrink_to_fit(&mut self)

Try to shrink the allocated data for this Int.

fn to_str_radix(&self, base: u8, upper: bool) -> String

Returns a string containing the value of self in base base. For bases greater than ten, if upper is true, upper-case letters are used, otherwise lower-case ones are used.

Panics if base is less than two or greater than 36.

fn write_radix<W: Write>(&self, w: &mut W, base: u8, upper: bool) -> Result<()>

fn from_str_radix(src: &str, base: u8) -> Result<Int, ParseIntError>

Creates a new Int from the given string in base base.

fn divmod(&self, other: &Int) -> (Int, Int)

Divide self by other, returning the quotient, Q, and remainder, R as (Q, R).

With N = self, D = other, Q and R satisfy: N = QD + R.

This will panic if other is zero.

fn pow(&self, exp: usize) -> Int

Raises self to the power of exp

fn square(&self) -> Int

Returns the square of self.

fn dsquare(self) -> Int

fn sqrt_rem(self) -> Option<(Int, Int)>

Compute the sqrt of this number, returning its floor, S, and the remainder, R, as Some((S, R)), or None if this number is negative.

The numbers S, R are both positive and satisfy self = S * S + R.

fn negate(&mut self)

Negates self in-place

fn is_even(&self) -> bool

Returns whether or not this number is even.

Returns 0 if self == 0

fn trailing_zeros(&self) -> u32

Returns the number of trailing zero bits in this number

Returns 0 if self == 0

fn count_ones(&self) -> usize

Returns the number of ones (the population count) in this number

If this number is negative, it has infinitely many ones (in two's complement), so this returns usize::MAX.

fn bit_length(&self) -> u32

Returns the number of bits required to represent (the absolute value of) this number, that is, floor(log2(abs(self))) + 1.

Returns 1 if self == 0.

fn bit(&self, bit: u32) -> bool

Returns the value of the bitth bit in this number, as if it were represented in two's complement.

fn set_bit(&mut self, bit: u32, bit_val: bool)

Set the bitth bit of this number to bit_val, treating negative numbers as if they're stored in two's complement.

fn gcd(&self, other: &Int) -> Int

Calculates the Greatest Common Divisor (GCD) of the number and other.

The result is always positive.

fn lcm(&self, other: &Int) -> Int

Calculates the Lowest Common Multiple (LCM) of the number and other.

Trait Implementations

impl Clone for Int

fn clone(&self) -> Int

fn clone_from(&mut self, other: &Int)

impl Default for Int

fn default() -> Int

impl Drop for Int

fn drop(&mut self)

impl PartialEq<Int> for Int

fn eq(&self, other: &Int) -> bool

fn ne(&self, other: &Rhs) -> bool

impl PartialEq<Limb> for Int

fn eq(&self, other: &Limb) -> bool

fn ne(&self, other: &Rhs) -> bool

impl Eq for Int

impl Ord for Int

fn cmp(&self, other: &Int) -> Ordering

impl PartialOrd<Int> for Int

fn partial_cmp(&self, other: &Int) -> Option<Ordering>

fn lt(&self, other: &Rhs) -> bool

fn le(&self, other: &Rhs) -> bool

fn gt(&self, other: &Rhs) -> bool

fn ge(&self, other: &Rhs) -> bool

impl PartialOrd<Limb> for Int

fn partial_cmp(&self, other: &Limb) -> Option<Ordering>

fn lt(&self, other: &Rhs) -> bool

fn le(&self, other: &Rhs) -> bool

fn gt(&self, other: &Rhs) -> bool

fn ge(&self, other: &Rhs) -> bool

impl Hash for Int

fn hash<H>(&self, state: &mut H) where H: Hasher

fn hash_slice<H>(data: &[Self], state: &mut H) where H: Hasher

impl AddAssign<Limb> for Int

fn add_assign(&mut self, other: Limb)

impl Add<Limb> for Int

type Output = Int

fn add(self, other: Limb) -> Int

impl<'a> AddAssign<&'a Int> for Int

fn add_assign(&mut self, other: &'a Int)

impl<'a> Add<&'a Int> for Int

type Output = Int

fn add(self, other: &'a Int) -> Int

impl<'a> Add<Int> for &'a Int

type Output = Int

fn add(self, other: Int) -> Int

impl Add<Int> for Int

type Output = Int

fn add(self, other: Int) -> Int

impl AddAssign<Int> for Int

fn add_assign(&mut self, other: Int)

impl<'a, 'b> Add<&'a Int> for &'b Int

type Output = Int

fn add(self, other: &'a Int) -> Int

impl SubAssign<Limb> for Int

fn sub_assign(&mut self, other: Limb)

impl Sub<Limb> for Int

type Output = Int

fn sub(self, other: Limb) -> Int

impl<'a> SubAssign<&'a Int> for Int

fn sub_assign(&mut self, other: &'a Int)

impl<'a> Sub<&'a Int> for Int

type Output = Int

fn sub(self, other: &'a Int) -> Int

impl<'a> Sub<Int> for &'a Int

type Output = Int

fn sub(self, other: Int) -> Int

impl Sub<Int> for Int

type Output = Int

fn sub(self, other: Int) -> Int

impl SubAssign<Int> for Int

fn sub_assign(&mut self, other: Int)

impl<'a, 'b> Sub<&'a Int> for &'b Int

type Output = Int

fn sub(self, other: &'a Int) -> Int

impl MulAssign<Limb> for Int

fn mul_assign(&mut self, other: Limb)

impl Mul<Limb> for Int

type Output = Int

fn mul(self, other: Limb) -> Int

impl<'a, 'b> Mul<&'a Int> for &'b Int

type Output = Int

fn mul(self, other: &'a Int) -> Int

impl<'a> Mul<&'a Int> for Int

type Output = Int

fn mul(self, other: &'a Int) -> Int

impl<'a> Mul<Int> for &'a Int

type Output = Int

fn mul(self, other: Int) -> Int

impl Mul<Int> for Int

type Output = Int

fn mul(self, other: Int) -> Int

impl<'a> MulAssign<&'a Int> for Int

fn mul_assign(&mut self, other: &'a Int)

impl MulAssign<Int> for Int

fn mul_assign(&mut self, other: Int)

impl DivAssign<Limb> for Int

fn div_assign(&mut self, other: Limb)

impl Div<Limb> for Int

type Output = Int

fn div(self, other: Limb) -> Int

impl<'a, 'b> Div<&'a Int> for &'b Int

type Output = Int

fn div(self, other: &'a Int) -> Int

impl<'a> Div<&'a Int> for Int

type Output = Int

fn div(self, other: &'a Int) -> Int

impl<'a> Div<Int> for &'a Int

type Output = Int

fn div(self, other: Int) -> Int

impl Div<Int> for Int

type Output = Int

fn div(self, other: Int) -> Int

impl<'a> DivAssign<&'a Int> for Int

fn div_assign(&mut self, other: &'a Int)

impl DivAssign<Int> for Int

fn div_assign(&mut self, other: Int)

impl Rem<Limb> for Int

type Output = Int

fn rem(self, other: Limb) -> Int

impl RemAssign<Limb> for Int

fn rem_assign(&mut self, other: Limb)

impl<'a, 'b> Rem<&'a Int> for &'b Int

type Output = Int

fn rem(self, other: &'a Int) -> Int

impl<'a> Rem<&'a Int> for Int

type Output = Int

fn rem(self, other: &'a Int) -> Int

impl<'a> Rem<Int> for &'a Int

type Output = Int

fn rem(self, other: Int) -> Int

impl Rem<Int> for Int

type Output = Int

fn rem(self, other: Int) -> Int

impl RemAssign<Int> for Int

fn rem_assign(&mut self, other: Int)

impl<'a> RemAssign<&'a Int> for Int

fn rem_assign(&mut self, other: &'a Int)

impl Neg for Int

type Output = Int

fn neg(self) -> Int

impl<'a> Neg for &'a Int

type Output = Int

fn neg(self) -> Int

impl ShlAssign<usize> for Int

fn shl_assign(&mut self, cnt: usize)

impl<'a> Shl<usize> for &'a Int

type Output = Int

fn shl(self, cnt: usize) -> Int

impl Shl<usize> for Int

type Output = Int

fn shl(self, other: usize) -> Int

impl ShrAssign<usize> for Int

fn shr_assign(&mut self, cnt: usize)

impl<'a> Shr<usize> for &'a Int

type Output = Int

fn shr(self, other: usize) -> Int

impl Shr<usize> for Int

type Output = Int

fn shr(self, other: usize) -> Int

impl<'a> BitAnd<Limb> for Int

type Output = Int

fn bitand(self, other: Limb) -> Int

impl BitAndAssign<Limb> for Int

fn bitand_assign(&mut self, other: Limb)

impl<'a> BitAnd<&'a Int> for Int

type Output = Int

fn bitand(self, other: &'a Int) -> Int

impl<'a> BitAnd<Int> for &'a Int

type Output = Int

fn bitand(self, other: Int) -> Int

impl<'a, 'b> BitAnd<&'a Int> for &'b Int

type Output = Int

fn bitand(self, other: &'a Int) -> Int

impl BitAnd<Int> for Int

type Output = Int

fn bitand(self, other: Int) -> Int

impl BitAndAssign<Int> for Int

fn bitand_assign(&mut self, other: Int)

impl<'a> BitAndAssign<&'a Int> for Int

fn bitand_assign(&mut self, other: &'a Int)

impl BitOr<Limb> for Int

type Output = Int

fn bitor(self, other: Limb) -> Int

impl BitOrAssign<Limb> for Int

fn bitor_assign(&mut self, other: Limb)

impl<'a> BitOr<&'a Int> for Int

type Output = Int

fn bitor(self, other: &'a Int) -> Int

impl<'a> BitOr<Int> for &'a Int

type Output = Int

fn bitor(self, other: Int) -> Int

impl<'a, 'b> BitOr<&'a Int> for &'b Int

type Output = Int

fn bitor(self, other: &'a Int) -> Int

impl BitOr<Int> for Int

type Output = Int

fn bitor(self, other: Int) -> Int

impl BitOrAssign<Int> for Int

fn bitor_assign(&mut self, other: Int)

impl<'a> BitOrAssign<&'a Int> for Int

fn bitor_assign(&mut self, other: &'a Int)

impl<'a> BitXor<Limb> for Int

type Output = Int

fn bitxor(self, other: Limb) -> Int

impl BitXorAssign<Limb> for Int

fn bitxor_assign(&mut self, other: Limb)

impl<'a> BitXor<&'a Int> for Int

type Output = Int

fn bitxor(self, other: &'a Int) -> Int

impl<'a> BitXor<Int> for &'a Int

type Output = Int

fn bitxor(self, other: Int) -> Int

impl<'a, 'b> BitXor<&'a Int> for &'b Int

type Output = Int

fn bitxor(self, other: &'a Int) -> Int

impl BitXor<Int> for Int

type Output = Int

fn bitxor(self, other: Int) -> Int

impl BitXorAssign<Int> for Int

fn bitxor_assign(&mut self, other: Int)

impl<'a> BitXorAssign<&'a Int> for Int

fn bitxor_assign(&mut self, other: &'a Int)

impl Add<i32> for Int

type Output = Int

fn add(self, other: i32) -> Int

impl AddAssign<i32> for Int

fn add_assign(&mut self, other: i32)

impl Sub<i32> for Int

type Output = Int

fn sub(self, other: i32) -> Int

impl SubAssign<i32> for Int

fn sub_assign(&mut self, other: i32)

impl Mul<i32> for Int

type Output = Int

fn mul(self, other: i32) -> Int

impl MulAssign<i32> for Int

fn mul_assign(&mut self, other: i32)

impl DivAssign<i32> for Int

fn div_assign(&mut self, other: i32)

impl Div<i32> for Int

type Output = Int

fn div(self, other: i32) -> Int

impl RemAssign<i32> for Int

fn rem_assign(&mut self, other: i32)

impl Rem<i32> for Int

type Output = Int

fn rem(self, other: i32) -> Int

impl BitAndAssign<i32> for Int

fn bitand_assign(&mut self, other: i32)

impl BitOrAssign<i32> for Int

fn bitor_assign(&mut self, other: i32)

impl BitXorAssign<i32> for Int

fn bitxor_assign(&mut self, other: i32)

impl<'a> Add<i32> for &'a Int

type Output = Int

fn add(self, other: i32) -> Int

impl<'a> Sub<i32> for &'a Int

type Output = Int

fn sub(self, other: i32) -> Int

impl<'a> Mul<i32> for &'a Int

type Output = Int

fn mul(self, other: i32) -> Int

impl<'a> Div<i32> for &'a Int

type Output = Int

fn div(self, other: i32) -> Int

impl<'a> Rem<i32> for &'a Int

type Output = Int

fn rem(self, other: i32) -> Int

impl BitAnd<i32> for Int

type Output = Int

fn bitand(self, other: i32) -> Int

impl<'a> BitAnd<i32> for &'a Int

type Output = Int

fn bitand(self, other: i32) -> Int

impl BitOr<i32> for Int

type Output = Int

fn bitor(self, other: i32) -> Int

impl<'a> BitOr<i32> for &'a Int

type Output = Int

fn bitor(self, other: i32) -> Int

impl BitXor<i32> for Int

type Output = Int

fn bitxor(self, other: i32) -> Int

impl<'a> BitXor<i32> for &'a Int

type Output = Int

fn bitxor(self, other: i32) -> Int

impl Add<usize> for Int

type Output = Int

fn add(self, other: usize) -> Int

impl AddAssign<usize> for Int

fn add_assign(&mut self, other: usize)

impl Sub<usize> for Int

type Output = Int

fn sub(self, other: usize) -> Int

impl SubAssign<usize> for Int

fn sub_assign(&mut self, other: usize)

impl Mul<usize> for Int

type Output = Int

fn mul(self, other: usize) -> Int

impl MulAssign<usize> for Int

fn mul_assign(&mut self, other: usize)

impl Div<usize> for Int

type Output = Int

fn div(self, other: usize) -> Int

impl DivAssign<usize> for Int

fn div_assign(&mut self, other: usize)

impl Rem<usize> for Int

type Output = Int

fn rem(self, other: usize) -> Int

impl RemAssign<usize> for Int

fn rem_assign(&mut self, other: usize)

impl BitAndAssign<usize> for Int

fn bitand_assign(&mut self, other: usize)

impl BitOrAssign<usize> for Int

fn bitor_assign(&mut self, other: usize)

impl BitXorAssign<usize> for Int

fn bitxor_assign(&mut self, other: usize)

impl<'a> Add<usize> for &'a Int

type Output = Int

fn add(self, other: usize) -> Int

impl<'a> Sub<usize> for &'a Int

type Output = Int

fn sub(self, other: usize) -> Int

impl<'a> Mul<usize> for &'a Int

type Output = Int

fn mul(self, other: usize) -> Int

impl<'a> Div<usize> for &'a Int

type Output = Int

fn div(self, other: usize) -> Int

impl<'a> Rem<usize> for &'a Int

type Output = Int

fn rem(self, other: usize) -> Int

impl BitAnd<usize> for Int

type Output = Int

fn bitand(self, other: usize) -> Int

impl<'a> BitAnd<usize> for &'a Int

type Output = Int

fn bitand(self, other: usize) -> Int

impl BitOr<usize> for Int

type Output = Int

fn bitor(self, other: usize) -> Int

impl<'a> BitOr<usize> for &'a Int

type Output = Int

fn bitor(self, other: usize) -> Int

impl BitXor<usize> for Int

type Output = Int

fn bitxor(self, other: usize) -> Int

impl<'a> BitXor<usize> for &'a Int

type Output = Int

fn bitxor(self, other: usize) -> Int

impl Add<BaseInt> for Int

type Output = Int

fn add(self, other: BaseInt) -> Int

impl AddAssign<BaseInt> for Int

fn add_assign(&mut self, other: BaseInt)

impl Sub<BaseInt> for Int

type Output = Int

fn sub(self, other: BaseInt) -> Int

impl SubAssign<BaseInt> for Int

fn sub_assign(&mut self, other: BaseInt)

impl Mul<BaseInt> for Int

type Output = Int

fn mul(self, other: BaseInt) -> Int

impl MulAssign<BaseInt> for Int

fn mul_assign(&mut self, other: BaseInt)

impl Div<BaseInt> for Int

type Output = Int

fn div(self, other: BaseInt) -> Int

impl DivAssign<BaseInt> for Int

fn div_assign(&mut self, other: BaseInt)

impl Rem<BaseInt> for Int

type Output = Int

fn rem(self, other: BaseInt) -> Int

impl RemAssign<BaseInt> for Int

fn rem_assign(&mut self, other: BaseInt)

impl BitAndAssign<BaseInt> for Int

fn bitand_assign(&mut self, other: BaseInt)

impl BitOrAssign<BaseInt> for Int

fn bitor_assign(&mut self, other: BaseInt)

impl BitXorAssign<BaseInt> for Int

fn bitxor_assign(&mut self, other: BaseInt)

impl<'a> Add<BaseInt> for &'a Int

type Output = Int

fn add(self, other: BaseInt) -> Int

impl<'a> Sub<BaseInt> for &'a Int

type Output = Int

fn sub(self, other: BaseInt) -> Int

impl<'a> Mul<BaseInt> for &'a Int

type Output = Int

fn mul(self, other: BaseInt) -> Int

impl<'a> Div<BaseInt> for &'a Int

type Output = Int

fn div(self, other: BaseInt) -> Int

impl<'a> Rem<BaseInt> for &'a Int

type Output = Int

fn rem(self, other: BaseInt) -> Int

impl BitAnd<BaseInt> for Int

type Output = Int

fn bitand(self, other: BaseInt) -> Int

impl<'a> BitAnd<BaseInt> for &'a Int

type Output = Int

fn bitand(self, other: BaseInt) -> Int

impl BitOr<BaseInt> for Int

type Output = Int

fn bitor(self, other: BaseInt) -> Int

impl<'a> BitOr<BaseInt> for &'a Int

type Output = Int

fn bitor(self, other: BaseInt) -> Int

impl BitXor<BaseInt> for Int

type Output = Int

fn bitxor(self, other: BaseInt) -> Int

impl<'a> BitXor<BaseInt> for &'a Int

type Output = Int

fn bitxor(self, other: BaseInt) -> Int

impl PartialEq<i32> for Int

fn eq(&self, other: &i32) -> bool

fn ne(&self, other: &Rhs) -> bool

impl PartialOrd<i32> for Int

fn partial_cmp(&self, other: &i32) -> Option<Ordering>

fn lt(&self, other: &Rhs) -> bool

fn le(&self, other: &Rhs) -> bool

fn gt(&self, other: &Rhs) -> bool

fn ge(&self, other: &Rhs) -> bool

impl PartialEq<usize> for Int

fn eq(&self, other: &usize) -> bool

fn ne(&self, other: &Rhs) -> bool

impl PartialOrd<usize> for Int

fn partial_cmp(&self, other: &usize) -> Option<Ordering>

fn lt(&self, other: &Rhs) -> bool

fn le(&self, other: &Rhs) -> bool

fn gt(&self, other: &Rhs) -> bool

fn ge(&self, other: &Rhs) -> bool

impl PartialEq<u64> for Int

fn eq(&self, other: &u64) -> bool

fn ne(&self, other: &Rhs) -> bool

impl PartialOrd<u64> for Int

fn partial_cmp(&self, other: &u64) -> Option<Ordering>

fn lt(&self, other: &Rhs) -> bool

fn le(&self, other: &Rhs) -> bool

fn gt(&self, other: &Rhs) -> bool

fn ge(&self, other: &Rhs) -> bool

impl PartialEq<i64> for Int

fn eq(&self, other: &i64) -> bool

fn ne(&self, other: &Rhs) -> bool

impl PartialOrd<i64> for Int

fn partial_cmp(&self, other: &i64) -> Option<Ordering>

fn lt(&self, other: &Rhs) -> bool

fn le(&self, other: &Rhs) -> bool

fn gt(&self, other: &Rhs) -> bool

fn ge(&self, other: &Rhs) -> bool

impl From<i8> for Int

fn from(val: i8) -> Int

impl From<i16> for Int

fn from(val: i16) -> Int

impl From<i32> for Int

fn from(val: i32) -> Int

impl From<i64> for Int

fn from(val: i64) -> Int

impl From<isize> for Int

fn from(val: isize) -> Int

impl From<u8> for Int

fn from(val: u8) -> Int

impl From<u16> for Int

fn from(val: u16) -> Int

impl From<u32> for Int

fn from(val: u32) -> Int

impl From<u64> for Int

fn from(val: u64) -> Int

impl From<usize> for Int

fn from(val: usize) -> Int

impl Binary for Int

fn fmt(&self, f: &mut Formatter) -> Result

impl Octal for Int

fn fmt(&self, f: &mut Formatter) -> Result

impl Display for Int

fn fmt(&self, f: &mut Formatter) -> Result

impl Debug for Int

fn fmt(&self, f: &mut Formatter) -> Result

impl LowerHex for Int

fn fmt(&self, f: &mut Formatter) -> Result

impl UpperHex for Int

fn fmt(&self, f: &mut Formatter) -> Result

impl FromStr for Int

type Err = ParseIntError

fn from_str(src: &str) -> Result<Int, ParseIntError>

impl Zero for Int

fn zero() -> Int

impl One for Int

fn one() -> Int

impl Step for Int

fn step(&self, by: &Int) -> Option<Int>

fn steps_between(start: &Int, end: &Int, by: &Int) -> Option<usize>