pub struct Int(/* private fields */);
Expand description
An integer limited to the range of integers that can be represented exactly by an f64.
Implementations§
Source§impl Int
impl Int
Sourcepub const MIN: Int = _
pub const MIN: Int = _
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());
Sourcepub const MAX: Int = _
pub const MAX: Int = _
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());
Sourcepub fn new(val: i64) -> Option<Int>
pub fn new(val: i64) -> Option<Int>
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);
Sourcepub fn new_saturating(val: i64) -> Int
pub fn new_saturating(val: i64) -> Int
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);
Sourcepub fn from_str_radix(src: &str, radix: u32) -> Result<Int, ParseIntError>
pub fn from_str_radix(src: &str, radix: u32) -> Result<Int, ParseIntError>
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)));
Sourcepub const fn min_value() -> Int
👎Deprecated: Use UInt::MIN
instead.
pub const fn min_value() -> Int
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());
Sourcepub const fn max_value() -> Int
👎Deprecated: Use Int::MAX
instead.
pub const fn max_value() -> Int
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());
Sourcepub fn abs(self) -> Int
pub fn abs(self) -> Int
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);
Sourcepub const fn is_positive(self) -> bool
pub const fn is_positive(self) -> bool
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());
Sourcepub const fn is_negative(self) -> bool
pub const fn is_negative(self) -> bool
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());
Sourcepub fn checked_add(self, rhs: Int) -> Option<Int>
pub fn checked_add(self, rhs: Int) -> Option<Int>
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);
Sourcepub fn checked_sub(self, rhs: Int) -> Option<Int>
pub fn checked_sub(self, rhs: Int) -> Option<Int>
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);
Sourcepub fn checked_mul(self, rhs: Int) -> Option<Int>
pub fn checked_mul(self, rhs: Int) -> Option<Int>
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);
Sourcepub fn checked_div(self, rhs: Int) -> Option<Int>
pub fn checked_div(self, rhs: Int) -> Option<Int>
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);
Sourcepub fn checked_rem(self, rhs: Int) -> Option<Int>
pub fn checked_rem(self, rhs: Int) -> Option<Int>
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)));
Sourcepub fn checked_pow(self, exp: u32) -> Option<Int>
pub fn checked_pow(self, exp: u32) -> Option<Int>
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);
Sourcepub fn saturating_add(self, rhs: Int) -> Int
pub fn saturating_add(self, rhs: Int) -> Int
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);
Sourcepub fn saturating_sub(self, rhs: Int) -> Int
pub fn saturating_sub(self, rhs: Int) -> Int
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);
Sourcepub fn saturating_mul(self, rhs: Int) -> Int
pub fn saturating_mul(self, rhs: Int) -> Int
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);
Sourcepub fn saturating_pow(self, exp: u32) -> Int
pub fn saturating_pow(self, exp: u32) -> Int
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§
Source§impl AddAssign for Int
impl AddAssign for Int
Source§fn add_assign(&mut self, other: Int)
fn add_assign(&mut self, other: Int)
+=
operation. Read moreSource§impl<'de> Deserialize<'de> for Int
impl<'de> Deserialize<'de> for Int
Source§fn deserialize<D>(
deserializer: D,
) -> Result<Int, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<Int, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Source§impl DivAssign for Int
impl DivAssign for Int
Source§fn div_assign(&mut self, other: Int)
fn div_assign(&mut self, other: Int)
/=
operation. Read moreSource§impl From<Int> for CanonicalJsonValue
impl From<Int> for CanonicalJsonValue
Source§fn from(val: Int) -> CanonicalJsonValue
fn from(val: Int) -> CanonicalJsonValue
Source§impl From<Int> for FlattenedJsonValue
impl From<Int> for FlattenedJsonValue
Source§fn from(value: Int) -> FlattenedJsonValue
fn from(value: Int) -> FlattenedJsonValue
Source§impl From<Int> for ScalarJsonValue
impl From<Int> for ScalarJsonValue
Source§fn from(value: Int) -> ScalarJsonValue
fn from(value: Int) -> ScalarJsonValue
Source§impl MulAssign for Int
impl MulAssign for Int
Source§fn mul_assign(&mut self, other: Int)
fn mul_assign(&mut self, other: Int)
*=
operation. Read moreSource§impl Ord for Int
impl Ord for Int
Source§impl PartialEq<CanonicalJsonValue> for Int
impl PartialEq<CanonicalJsonValue> for Int
Source§impl PartialEq<Int> for CanonicalJsonValue
impl PartialEq<Int> for CanonicalJsonValue
Source§impl PartialOrd for Int
impl PartialOrd for Int
Source§impl RemAssign for Int
impl RemAssign for Int
Source§fn rem_assign(&mut self, other: Int)
fn rem_assign(&mut self, other: Int)
%=
operation. Read moreSource§impl Serialize for Int
impl Serialize for Int
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
Source§impl SubAssign for Int
impl SubAssign for Int
Source§fn sub_assign(&mut self, other: Int)
fn sub_assign(&mut self, other: Int)
-=
operation. Read moreimpl Copy for Int
impl Eq for Int
impl StructuralPartialEq for Int
Auto Trait Implementations§
impl Freeze for Int
impl RefUnwindSafe for Int
impl Send for Int
impl Sync for Int
impl Unpin for Int
impl UnwindSafe for Int
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.Source§impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
Source§impl<T> Identity for Twhere
T: ?Sized,
impl<T> Identity for Twhere
T: ?Sized,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more