Type ::std::i64

The primitive integer type.

Methods

fn to<f64>(self) -> f64

Convert an int to a float.

Examples

assert!(10.to::<f64>() is f64);
fn max(self, other: i64) -> i64

Compares and returns the maximum of two values.

Returns the second argument if the comparison determines them to be equal.

Examples

assert_eq!(2, 1.max(2));
assert_eq!(2, 2.max(2));
fn min(self, other: i64) -> i64

Compares and returns the minimum of two values.

Returns the first argument if the comparison determines them to be equal.

Examples

assert_eq!(1, 1.min(2));
assert_eq!(2, 2.min(2));
fn abs(self) -> i64

Computes the absolute value of self.

Overflow behavior

The absolute value of i64::MIN cannot be represented as an int, and attempting to calculate it will cause an overflow. This means that such code will wrap to i64::MIN without a panic.

Examples

Basic usage:

assert_eq!(10.abs(), 10);
assert_eq!((-10).abs(), 10);
fn pow(self, pow: i64) -> i64

Raises self to the power of exp, using exponentiation by squaring.

Overflow behavior

This function will wrap on overflow.

Examples

Basic usage:

let x = 2;

assert_eq!(x.pow(5), 32);
fn checked_add(self, rhs: i64) -> Option

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

Examples

Basic usage:

assert_eq!((i64::MAX - 2).checked_add(1), Some(i64::MAX - 1));
assert_eq!((i64::MAX - 2).checked_add(3), None);
fn checked_sub(self, rhs: i64) -> Option

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

Examples

Basic usage:

assert_eq!((i64::MIN + 2).checked_sub(1), Some(i64::MIN + 1));
assert_eq!((i64::MIN + 2).checked_sub(3), None);
fn checked_div(self, rhs: i64) -> Option

Checked integer division. Computes self / rhs, returning None if rhs == 0 or the division results in overflow.

Examples

Basic usage:

assert_eq!((i64::MIN + 1).checked_div(-1), Some(i64::MAX));
assert_eq!(i64::MIN.checked_div(-1), None);
assert_eq!((1).checked_div(0), None);
fn checked_mul(self, rhs: i64) -> Option

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

Examples

Basic usage:

assert_eq!(i64::MAX.checked_mul(1), Some(i64::MAX));
assert_eq!(i64::MAX.checked_mul(2), None);
fn checked_rem(self, rhs: i64) -> Option

Checked integer remainder. Computes self % rhs, returning None if rhs == 0 or the division results in overflow.

Examples

Basic usage:

assert_eq!(5.checked_rem(2), Some(1));
assert_eq!(5.checked_rem(0), None);
assert_eq!(i64::MIN.checked_rem(-1), None);
fn wrapping_add(self, rhs: i64) -> i64

Wrapping (modular) addition. Computes self + rhs, wrapping around at the boundary of the type.

Examples

Basic usage:

assert_eq!(100.wrapping_add(27), 127);
assert_eq!(i64::MAX.wrapping_add(2), i64::MIN + 1);
fn wrapping_sub(self, rhs: i64) -> i64

Wrapping (modular) subtraction. Computes self - rhs, wrapping around at the boundary of the type.

Examples

Basic usage:

fn wrapping_div(self, rhs: i64) -> i64

Wrapping (modular) division. Computes self / rhs, wrapping around at the boundary of the type.

The only case where such wrapping can occur is when one divides MIN / -1 on a signed type (where MIN is the negative minimal value for the type); this is equivalent to -MIN, a positive value that is too large to represent in the type. In such a case, this function returns MIN itself.

Panics

This function will panic if rhs is 0.

Examples

Basic usage:

assert_eq!(100.wrapping_div(10), 10);
fn wrapping_mul(self, rhs: i64) -> i64

Wrapping (modular) multiplication. Computes self * rhs, wrapping around at the boundary of the type.

Examples

Basic usage:

assert_eq!(10.wrapping_mul(12), 120);
fn wrapping_rem(self, rhs: i64) -> i64

Wrapping (modular) remainder. Computes self % rhs, wrapping around at the boundary of the type.

Such wrap-around never actually occurs mathematically; implementation artifacts make x % y invalid for MIN / -1 on a signed type (where MIN is the negative minimal value). In such a case, this function returns 0.

Panics

This function will panic if rhs is 0.

Examples

Basic usage:

assert_eq!(100.wrapping_rem(10), 0);
fn saturating_add(self, rhs: i64) -> i64

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

Examples

Basic usage:

assert_eq!(100.saturating_add(1), 101);
assert_eq!(i64::MAX.saturating_add(100), i64::MAX);
assert_eq!(i64::MIN.saturating_add(-1), i64::MIN);
fn saturating_sub(self, rhs: i64) -> i64

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

Examples

Basic usage:

assert_eq!(100.saturating_sub(127), -27);
assert_eq!(i64::MIN.saturating_sub(100), i64::MIN);
assert_eq!(i64::MAX.saturating_sub(-1), i64::MAX);
fn saturating_mul(self, rhs: i64) -> i64

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

Examples

Basic usage:

assert_eq!(10.saturating_mul(12), 120);
assert_eq!(i64::MAX.saturating_mul(10), i64::MAX);
assert_eq!(i64::MIN.saturating_mul(10), i64::MIN);

Saturating absolute value. Computes self.abs(), returning MAX if self == MIN instead of overflowing.

Examples

Basic usage:

assert_eq!(100.saturating_abs(), 100);
assert_eq!((-100).saturating_abs(), 100);
assert_eq!(i64::MIN.saturating_abs(), i64::MAX);
assert_eq!((i64::MIN + 1).saturating_abs(), i64::MAX);
fn saturating_pow(self, rhs: i64) -> i64

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

Examples

Basic usage:

assert_eq!((-4).saturating_pow(3), -64);
assert_eq!(i64::MIN.saturating_pow(2), i64::MAX);
assert_eq!(i64::MIN.saturating_pow(3), i64::MIN);
fn signum(self) -> i64

Returns a number representing sign of self.

  • 0 if the number is zero
  • 1 if the number is positive
  • -1 if the number is negative

Examples

Basic usage:

assert_eq!(10.signum(), 1);
assert_eq!(0.signum(), 0);
assert_eq!((-10).signum(), -1);

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

Examples

Basic usage:

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

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

Examples

Basic usage:

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

Returns the number as a string.

Examples

Basic usage:

assert_eq!((-10).to_string(), "-10");
assert_eq!(10.to_string(), "10");

Protocols

protocol partial_eq
if value == b { }

Test two integers for partial equality.

Examples

use std::ops::partial_eq;

assert_eq!(partial_eq(5, 5), true);
assert_eq!(partial_eq(5, 10), false);
assert_eq!(partial_eq(10, 5), false);
protocol eq
if value == b { }

Test two integers for total equality.

Examples

use std::ops::eq;

assert_eq!(eq(5, 5), true);
assert_eq!(eq(5, 10), false);
assert_eq!(eq(10, 5), false);
protocol partial_cmp
if value < b { }

Perform a partial ordered comparison between two integers.

Examples

use std::cmp::Ordering;
use std::ops::partial_cmp;

assert_eq!(partial_cmp(5, 10), Some(Ordering::Less));
assert_eq!(partial_cmp(10, 5), Some(Ordering::Greater));
assert_eq!(partial_cmp(5, 5), Some(Ordering::Equal));
protocol cmp
if value < b { }

Perform a partial ordered comparison between two integers.

Examples

use std::cmp::Ordering;
use std::ops::cmp;

assert_eq!(cmp(5, 10), Ordering::Less);
assert_eq!(cmp(10, 5), Ordering::Greater);
assert_eq!(cmp(5, 5), Ordering::Equal);