Struct js_int::Int

source · []
pub struct Int(_);
Expand description

An integer limited to the range of integers that can be represented exactly by an f64.

Implementations

The smallest value that can be represented by this integer type.

Examples

Basic usage:

assert_eq!(Int::MIN, Int::try_from(-9_007_199_254_740_991i64).unwrap());

The largest value that can be represented by this integer type.

Examples

Basic usage:

assert_eq!(Int::MAX, Int::try_from(9_007_199_254_740_991i64).unwrap());

Try to create an Int from the provided i64, returning None if it is smaller than MIN_SAFE_INT or larger than MAX_SAFE_INT.

This is the same as the TryFrom<u64> implementation for Int, except that it returns an Option instead of a Result.

Examples

Basic usage:

assert_eq!(Int::new(js_int::MIN_SAFE_INT), Some(Int::MIN));
assert_eq!(Int::new(js_int::MAX_SAFE_INT), Some(Int::MAX));
assert_eq!(Int::new(js_int::MIN_SAFE_INT - 1), None);
assert_eq!(Int::new(js_int::MAX_SAFE_INT + 1), None);

Creates an Int from the given i64 clamped to the safe interval.

The given value gets clamped into the closed interval between MIN_SAFE_INT and MAX_SAFE_INT.

Examples

Basic usage:

assert_eq!(Int::new_saturating(0), int!(0));
assert_eq!(Int::new_saturating(js_int::MAX_SAFE_INT), Int::MAX);
assert_eq!(Int::new_saturating(js_int::MAX_SAFE_INT + 1), Int::MAX);
assert_eq!(Int::new_saturating(js_int::MIN_SAFE_INT), Int::MIN);
assert_eq!(Int::new_saturating(js_int::MIN_SAFE_INT - 1), Int::MIN);

Converts a string slice in a given base to an integer.

The string is expected to be an optional + or - sign followed by digits. Leading and trailing whitespace represent an error. Digits are a subset of these characters, depending on radix:

  • 0-9
  • a-z
  • A-Z
Panics

This function panics if radix is not in the range from 2 to 36.

Examples

Basic usage:

assert_eq!(Int::from_str_radix("A", 16), Ok(int!(10)));
👎 Deprecated:

Use UInt::MIN instead.

Returns the smallest value that can be represented by this integer type.

Examples

Basic usage:

assert_eq!(Int::min_value(), Int::try_from(-9_007_199_254_740_991i64).unwrap());
👎 Deprecated:

Use Int::MAX instead.

Returns the largest value that can be represented by this integer type.

Examples

Basic usage:

assert_eq!(Int::max_value(), Int::try_from(9_007_199_254_740_991i64).unwrap());

Computes the absolute value of self.

Examples

Basic usage:

assert_eq!(int!(10).abs(), int!(10));
assert_eq!(int!(-10).abs(), int!(10));

// Differently from i8 / i16 / i32 / i128, Int's min_value is its max_value negated
assert_eq!(Int::MIN.abs(), Int::MAX);

Returns true if self is positive and false if the number is zero or negative.

Examples

Basic usage:

assert!(int!(10).is_positive());
assert!(!int!(0).is_positive());
assert!(!int!(-10).is_positive());

Returns true if self is negative and false if the number is zero or positive.

Examples

Basic usage:

assert!(int!(-10).is_negative());
assert!(!int!(0).is_negative());
assert!(!int!(10).is_negative());

Checked integer addition. Computes self + rhs, returning None if overflow occurred.

Examples

Basic usage:

assert_eq!(
    (Int::MAX - int!(1)).checked_add(int!(1)),
    Some(Int::MAX)
);
assert_eq!((Int::MAX - int!(1)).checked_add(int!(2)), None);

Checked integer subtraction. Computes self - rhs, returning None if overflow occurred.

Examples

Basic usage:

assert_eq!(
    (Int::MIN + int!(2)).checked_sub(int!(1)),
    Some(Int::MIN + int!(1))
);
assert_eq!((Int::MIN + int!(2)).checked_sub(int!(3)), None);

Checked integer multiplication. Computes self * rhs, returning None if overflow occurred.

Examples

Basic usage:

assert_eq!(int!(5).checked_mul(int!(1)), Some(int!(5)));
assert_eq!(Int::MAX.checked_mul(int!(2)), None);

Checked integer division. Computes self / rhs, returning None if rhs == 0.

Examples

Basic usage:

assert_eq!(Int::MIN.checked_div(int!(-1)), Some(Int::MAX));
assert_eq!(int!(1).checked_div(int!(0)), None);

Checked integer remainder. Computes self % rhs, returning None if rhs == 0.

Examples

Basic usage:

assert_eq!(int!(5).checked_rem(int!(2)), Some(int!(1)));
assert_eq!(int!(5).checked_rem(int!(0)), None);
assert_eq!(Int::MIN.checked_rem(int!(-1)), Some(int!(0)));

Checked exponentiation. Computes self.pow(exp), returning None if overflow or underflow occurred.

Examples

Basic usage:

assert_eq!(int!(8).checked_pow(2), Some(int!(64)));
assert_eq!(Int::MAX.checked_pow(2), None);
assert_eq!(Int::MIN.checked_pow(2), None);
assert_eq!(int!(1_000_000_000).checked_pow(2), None);

Saturating integer addition. Computes self + rhs, saturating at the numeric bounds instead of overflowing.

Examples

Basic usage:

assert_eq!(int!(100).saturating_add(int!(1)), int!(101));
assert_eq!(Int::MAX.saturating_add(int!(1)), Int::MAX);
assert_eq!(Int::MIN.saturating_add(int!(-1)), Int::MIN);

Saturating integer subtraction. Computes self - rhs, saturating at the numeric bounds instead of underflowing.

Examples

Basic usage:

assert_eq!(int!(100).saturating_sub(int!(1)), int!(99));
assert_eq!(Int::MIN.saturating_sub(int!(1)), Int::MIN);
assert_eq!(Int::MAX.saturating_sub(int!(-1)), Int::MAX);

Saturating integer multiplication. Computes self * rhs, saturating at the numeric bounds instead of overflowing.

Examples

Basic usage:

assert_eq!(int!(100).saturating_mul(int!(2)), int!(200));
assert_eq!(Int::MAX.saturating_mul(int!(2)), Int::MAX);
assert_eq!(Int::MAX.saturating_mul(Int::MAX), Int::MAX);
assert_eq!(Int::MAX.saturating_mul(Int::MIN), Int::MIN);

Saturating integer exponentiation. Computes self.pow(exp), saturating at the numeric bounds instead of overflowing or underflowing.

Examples

Basic usage:

assert_eq!(int!(5).saturating_pow(2), int!(25));
assert_eq!(int!(-2).saturating_pow(3), int!(-8));
assert_eq!(Int::MAX.saturating_pow(2), Int::MAX);
assert_eq!(Int::MIN.saturating_pow(2), Int::MAX);

Trait Implementations

The resulting type after applying the + operator.

Performs the + operation. Read more

Performs the += operation. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Formats the value using the given formatter. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

Performs the /= operation. Read more

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

The associated error which can be returned from parsing.

Parses a string s to return a value of this type. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

Performs the *= operation. Read more

The resulting type after applying the - operator.

Performs the unary - operation. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Method which takes an iterator and generates Self from the elements by multiplying the items. Read more

Method which takes an iterator and generates Self from the elements by multiplying the items. Read more

The resulting type after applying the % operator.

Performs the % operation. Read more

Performs the %= operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

Performs the -= operation. Read more

Method which takes an iterator and generates Self from the elements by “summing up” the items. Read more

Method which takes an iterator and generates Self from the elements by “summing up” the items. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.