Primitive Type u641.0.0[]

Expand description

64 位无符号整数类型。

Implementations

此整数类型可以表示的最小值。

Examples

基本用法:

assert_eq!(u64::MIN, 0);
Run

此整数类型可以表示的最大值。

Examples

基本用法:

assert_eq!(u64::MAX, 18446744073709551615);
Run

此整数类型的大小 (以位为单位)。

Examples

assert_eq!(u64::BITS, 64);
Run

将给定基数的字符串切片转换为整数。

该字符串应为可选的 + 符号,后跟数字。

前导和尾随空格表示错误。 根据 radix,数字是这些字符的子集:

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

Panics

如果 radix 不在 2 到 36 之间,则此函数 panics。

Examples

基本用法:

assert_eq!(u64::from_str_radix("A", 16), Ok(10));
Run

返回 self 二进制表示形式中的位数。

Examples

基本用法:

let n = 0b01001100u64;

assert_eq!(n.count_ones(), 3);
Run

返回 self 二进制表示形式中的零数。

Examples

基本用法:

assert_eq!(u64::MAX.count_zeros(), 0);
Run

返回 self 二进制表示形式中前导零的数目。

Examples

基本用法:

let n = u64::MAX >> 2;

assert_eq!(n.leading_zeros(), 2);
Run

返回 self 二进制表示形式中的尾随零数。

Examples

基本用法:

let n = 0b0101000u64;

assert_eq!(n.trailing_zeros(), 3);
Run

返回 self 二进制表示形式中前导数字。

Examples

基本用法:

let n = !(u64::MAX >> 2);

assert_eq!(n.leading_ones(), 2);
Run

返回 self 二进制表示形式中的尾随数字。

Examples

基本用法:

let n = 0b1010111u64;

assert_eq!(n.trailing_ones(), 3);
Run

将位左移指定的量 n,将截断的位包装到结果整数的末尾。

请注意,此操作与 << 移位运算符不同!

Examples

基本用法:

let n = 0xaa00000000006e1u64;
let m = 0x6e10aa;

assert_eq!(n.rotate_left(12), m);
Run

将位右移指定的量 n,将截断的位包装到结果整数的开头。

请注意,此操作与 >> 移位运算符不同!

Examples

基本用法:

let n = 0x6e10aau64;
let m = 0xaa00000000006e1;

assert_eq!(n.rotate_right(12), m);
Run

反转整数的字节顺序。

Examples

基本用法:

let n = 0x1234567890123456u64;
let m = n.swap_bytes();

assert_eq!(m, 0x5634129078563412);
Run

反转整数中的位顺序。 最低有效位变为最高有效位,第二最低有效位变为第二最高有效位,依此类推。

Examples

基本用法:

let n = 0x1234567890123456u64;
let m = n.reverse_bits();

assert_eq!(m, 0x6a2c48091e6a2c48);
assert_eq!(0, 0u64.reverse_bits());
Run

将整数从大端字节序转换为目标的字节序。

在大端节序序上,这是个禁忌。 在小端字节序上,字节被交换。

Examples

基本用法:

let n = 0x1Au64;

if cfg!(target_endian = "big") {
    assert_eq!(u64::from_be(n), n)
} else {
    assert_eq!(u64::from_be(n), n.swap_bytes())
}
Run

将整数从小端字节序转换为目标的字节序。

在小端字节序上,这是个禁忌。 在大字节序中,字节被交换。

Examples

基本用法:

let n = 0x1Au64;

if cfg!(target_endian = "little") {
    assert_eq!(u64::from_le(n), n)
} else {
    assert_eq!(u64::from_le(n), n.swap_bytes())
}
Run

self 从目标的字节序转换为大字节序。

在大端节序序上,这是个禁忌。 在小端字节序上,字节被交换。

Examples

基本用法:

let n = 0x1Au64;

if cfg!(target_endian = "big") {
    assert_eq!(n.to_be(), n)
} else {
    assert_eq!(n.to_be(), n.swap_bytes())
}
Run

self 从目标的字节序转换为 Little Endian。

在小端字节序上,这是个禁忌。 在大字节序中,字节被交换。

Examples

基本用法:

let n = 0x1Au64;

if cfg!(target_endian = "little") {
    assert_eq!(n.to_le(), n)
} else {
    assert_eq!(n.to_le(), n.swap_bytes())
}
Run

检查整数加法。 计算 self + rhs,如果发生溢出则返回 None

Examples

基本用法:

assert_eq!((u64::MAX - 2).checked_add(1), Some(u64::MAX - 1));
assert_eq!((u64::MAX - 2).checked_add(3), None);
Run
🔬 This is a nightly-only experimental API. (unchecked_math #85122)

niche optimization path

未经检查的整数加法。 假设不会发生溢出,则计算 self + rhs

Safety

当以下情况时,这导致未定义的行为 self + rhs > u64::MAX or self + rhs < u64::MIN, i.e. 当 checked_add 返回 None 时。

检查整数减法。 计算 self - rhs,如果发生溢出则返回 None

Examples

基本用法:

assert_eq!(1u64.checked_sub(1), Some(0));
assert_eq!(0u64.checked_sub(1), None);
Run
🔬 This is a nightly-only experimental API. (unchecked_math #85122)

niche optimization path

未经检查的整数减法。 假设不会发生溢出,则计算 self - rhs

Safety

当以下情况时,这导致未定义的行为 self - rhs > u64::MAX or self - rhs < u64::MIN, i.e. 当 checked_sub 返回 None 时。

检查整数乘法。 计算 self * rhs,如果发生溢出则返回 None

Examples

基本用法:

assert_eq!(5u64.checked_mul(1), Some(5));
assert_eq!(u64::MAX.checked_mul(2), None);
Run
🔬 This is a nightly-only experimental API. (unchecked_math #85122)

niche optimization path

未经检查的整数乘法。 假设不会发生溢出,则计算 self * rhs

Safety

当以下情况时,这导致未定义的行为 self * rhs > u64::MAX or self * rhs < u64::MIN, i.e. 当 checked_mul 返回 None 时。

检查整数除法。 计算 self / rhs,如果为 rhs == 0,则返回 None

Examples

基本用法:

assert_eq!(128u64.checked_div(2), Some(64));
assert_eq!(1u64.checked_div(0), None);
Run

检查欧几里得除法。 计算 self.div_euclid(rhs),如果为 rhs == 0,则返回 None

Examples

基本用法:

assert_eq!(128u64.checked_div_euclid(2), Some(64));
assert_eq!(1u64.checked_div_euclid(0), None);
Run

检查整数余数。 计算 self % rhs,如果为 rhs == 0,则返回 None

Examples

基本用法:

assert_eq!(5u64.checked_rem(2), Some(1));
assert_eq!(5u64.checked_rem(0), None);
Run

检查欧几里德模数。 计算 self.rem_euclid(rhs),如果为 rhs == 0,则返回 None

Examples

基本用法:

assert_eq!(5u64.checked_rem_euclid(2), Some(1));
assert_eq!(5u64.checked_rem_euclid(0), None);
Run
🔬 This is a nightly-only experimental API. (int_log #70887)

返回数字相对于任意基数的对数。

由于实现细节,此方法可能未优化; log2 可以更有效地为基数 2 生成结果,而 log10 可以更有效地为基数 10 生成结果。

Panics

当数字为负数时,为零,或者基数不至少为 2; 它在调试模式下为 panics,在发布模式下返回值被包装为 0 (该方法可以返回 0 的唯一情况)。

Examples

#![feature(int_log)]
assert_eq!(5u64.log(5), 1);
Run
🔬 This is a nightly-only experimental API. (int_log #70887)

返回数字的以 2 为底的对数。

Panics

当数字为负数或零时,它在调试模式下为 panics,在发布模式下返回值包装为 0 (该方法可以返回 0 的唯一情况)。

Examples

#![feature(int_log)]
assert_eq!(2u64.log2(), 1);
Run
🔬 This is a nightly-only experimental API. (int_log #70887)

返回数字的以 10 为底的对数。

Panics

当数字为负数或零时,它在调试模式下为 panics,在发布模式下返回值包装为 0 (该方法可以返回 0 的唯一情况)。

Example

#![feature(int_log)]
assert_eq!(10u64.log10(), 1);
Run
🔬 This is a nightly-only experimental API. (int_log #70887)

返回数字相对于任意基数的对数。

如果数字为零,或者基数不至少为 2,则返回 None

由于实现细节,此方法可能未优化; checked_log2 可以更有效地为基数 2 生成结果,而 checked_log10 可以更有效地为基数 10 生成结果。

Examples

#![feature(int_log)]
assert_eq!(5u64.checked_log(5), Some(1));
Run
🔬 This is a nightly-only experimental API. (int_log #70887)

返回数字的以 2 为底的对数。

如果数字为零,则返回 None

Examples

#![feature(int_log)]
assert_eq!(2u64.checked_log2(), Some(1));
Run
🔬 This is a nightly-only experimental API. (int_log #70887)

返回数字的以 10 为底的对数。

如果数字为零,则返回 None

Examples

#![feature(int_log)]
assert_eq!(10u64.checked_log10(), Some(1));
Run

检查否定。 计算 -self,除非 self == 0,否则返回 None

请注意,取反任何正整数将溢出。

Examples

基本用法:

assert_eq!(0u64.checked_neg(), Some(0));
assert_eq!(1u64.checked_neg(), None);
Run

检查左移。 计算 self << rhs,如果 rhs 大于或等于 self 中的位数,则返回 None

Examples

基本用法:

assert_eq!(0x1u64.checked_shl(4), Some(0x10));
assert_eq!(0x10u64.checked_shl(129), None);
Run
🔬 This is a nightly-only experimental API. (unchecked_math #85122)

niche optimization path

未检查的左移。 计算 self << rhs,假设 rhs 小于 self 中的位数。

Safety

如果 rhs 大于或等于 self 中的位数,则会导致未定义的行为,即

checked_shl 返回 None 时。

检查右移。 计算 self >> rhs,如果 rhs 大于或等于 self 中的位数,则返回 None

Examples

基本用法:

assert_eq!(0x10u64.checked_shr(4), Some(0x1));
assert_eq!(0x10u64.checked_shr(129), None);
Run
🔬 This is a nightly-only experimental API. (unchecked_math #85122)

niche optimization path

未检查右移。 计算 self >> rhs,假设 rhs 小于 self 中的位数。

Safety

如果 rhs 大于或等于 self 中的位数,则会导致未定义的行为,即

checked_shr 返回 None 时。

检查取幂。 计算 self.pow(exp),如果发生溢出则返回 None

Examples

基本用法:

assert_eq!(2u64.checked_pow(5), Some(32));
assert_eq!(u64::MAX.checked_pow(2), None);
Run

饱和整数加法。 计算 self + rhs,在数字范围内饱和,而不是溢出。

Examples

基本用法:

assert_eq!(100u64.saturating_add(1), 101);
assert_eq!(u64::MAX.saturating_add(127), u64::MAX);
Run

饱和整数减法。 计算 self - rhs,在数字范围内饱和,而不是溢出。

Examples

基本用法:

assert_eq!(100u64.saturating_sub(27), 73);
assert_eq!(13u64.saturating_sub(127), 0);
Run

饱和整数乘法。 计算 self * rhs,在数字范围内饱和,而不是溢出。

Examples

基本用法:

assert_eq!(2u64.saturating_mul(10), 20);
assert_eq!((u64::MAX).saturating_mul(10), u64::MAX);
Run

饱和整数幂。 计算 self.pow(exp),在数字范围内饱和,而不是溢出。

Examples

基本用法:

assert_eq!(4u64.saturating_pow(3), 64);
assert_eq!(u64::MAX.saturating_pow(2), u64::MAX);
Run

包装 (modular) 添加。 计算 self + rhs,在类型的边界处回绕。

Examples

基本用法:

assert_eq!(200u64.wrapping_add(55), 255);
assert_eq!(200u64.wrapping_add(u64::MAX), 199);
Run

包装 (modular) 减法。 计算 self - rhs,在类型的边界处回绕。

Examples

基本用法:

assert_eq!(100u64.wrapping_sub(100), 0);
assert_eq!(100u64.wrapping_sub(u64::MAX), 101);
Run

包装 (modular) 乘法。 计算 self * rhs,在类型的边界处回绕。

Examples

基本用法:

请注意,此示例在整数类型之间共享。 这就解释了为什么在这里使用 u8

assert_eq!(10u8.wrapping_mul(12), 120);
assert_eq!(25u8.wrapping_mul(12), 44);
Run

包装 (modular) 分区。计算 self / rhs。 无符号类型的包装除法只是普通除法。 包装是不可能发生的。 该函数存在,因此所有操作都在包装操作中考虑。

Examples

基本用法:

assert_eq!(100u64.wrapping_div(10), 10);
Run

包装欧几里得除法。计算 self.div_euclid(rhs)。 无符号类型的包装除法只是普通除法。 包装是不可能发生的。 该函数存在,因此所有操作都在包装操作中考虑。 因为对于正整数,所有除法的通用定义都是相等的,所以它恰好等于 self.wrapping_div(rhs)

Examples

基本用法:

assert_eq!(100u64.wrapping_div_euclid(10), 10);
Run

包装 (modular) 余数。计算 self % rhs。 无符号类型的包装余数计算只是常规余数计算。

包装是不可能发生的。 该函数存在,因此所有操作都在包装操作中考虑。

Examples

基本用法:

assert_eq!(100u64.wrapping_rem(10), 0);
Run

包装欧几里德模。计算 self.rem_euclid(rhs)。 无符号类型的包装模运算只是常规的余数计算。 包装是不可能发生的。 该函数存在,因此所有操作都在包装操作中考虑。 因为对于正整数,所有除法的通用定义都是相等的,所以它恰好等于 self.wrapping_rem(rhs)

Examples

基本用法:

assert_eq!(100u64.wrapping_rem_euclid(10), 0);
Run

包装 (modular) 取反。 计算 -self,在类型的边界处回绕。

由于无符号类型没有负的等效项,因此该函数的所有应用程序都将自动换行 (-0 除外)。 对于小于相应有符号类型的最大值的值,结果与强制转换相应有符号值的结果相同。

任何较大的值都等于 MAX + 1 - (val - MAX - 1),其中 MAX 是对应的有符号类型的最大值。

Examples

基本用法:

请注意,此示例在整数类型之间共享。 这就解释了为什么在这里使用 i8

assert_eq!(100i8.wrapping_neg(), -100);
assert_eq!((-128i8).wrapping_neg(), -128);
Run

无 Panic - 按位左移; 产生 self << mask(rhs),其中 mask 删除 rhs 的所有高位,这些高位将导致移位超过该类型的位宽。

注意,这与左旋不同; 环绕左移的 RHS 限于该类型的范围,而不是从 LHS 移出的位返回到另一端。 所有原始整数类型都实现了 rotate_left 函数,而您可能想要的是 rotate_left 函数。

Examples

基本用法:

assert_eq!(1u64.wrapping_shl(7), 128);
assert_eq!(1u64.wrapping_shl(128), 1);
Run

无 Panic - 按位右移; 产生 self >> mask(rhs),其中 mask 删除 rhs 的所有高位,这些高位将导致移位超过该类型的位宽。

注意,这与右旋转不同。换行右移的 RHS 限于类型的范围,而不是从 LHS 移出的位返回到另一端。 所有原始整数类型都实现了 rotate_right 函数,而您可能想要的是 rotate_right 函数。

Examples

基本用法:

assert_eq!(128u64.wrapping_shr(7), 1);
assert_eq!(128u64.wrapping_shr(128), 128);
Run

包装 (modular) 指数。 计算 self.pow(exp),在类型的边界处回绕。

Examples

基本用法:

assert_eq!(3u64.wrapping_pow(5), 243);
assert_eq!(3u8.wrapping_pow(6), 217);
Run

计算 self + rhs

返回一个加法元组以及一个布尔值,该布尔值指示是否会发生算术溢出。 如果将发生溢出,则返回包装的值。

Examples

基本用法


assert_eq!(5u64.overflowing_add(2), (7, false));
assert_eq!(u64::MAX.overflowing_add(1), (0, true));
Run

计算 self-rhs

返回一个减法的元组以及一个布尔值,该布尔值指示是否会发生算术溢出。 如果将发生溢出,则返回包装的值。

Examples

基本用法


assert_eq!(5u64.overflowing_sub(2), (3, false));
assert_eq!(0u64.overflowing_sub(1), (u64::MAX, true));
Run

计算 selfrhs 的乘法。

返回乘法的元组以及一个布尔值,该布尔值指示是否会发生算术溢出。 如果将发生溢出,则返回包装的值。

Examples

基本用法:

请注意,此示例在整数类型之间共享。 这就解释了为什么在这里使用 u32

assert_eq!(5u32.overflowing_mul(2), (10, false));
assert_eq!(1_000_000_000u32.overflowing_mul(10), (1410065408, true));
Run

self 除以 rhs 时计算除数。

返回除数的元组以及指示是否将发生算术溢出的布尔值。 请注意,对于无符号整数,永远不会发生溢出,因此第二个值始终为 false

Panics

如果 rhs 为 0,则此函数将为 panic。

Examples

基本用法

assert_eq!(5u64.overflowing_div(2), (2, false));
Run

计算欧几里得除法 self.div_euclid(rhs) 的商。

返回除数的元组以及指示是否将发生算术溢出的布尔值。 请注意,对于无符号整数,永远不会发生溢出,因此第二个值始终为 false。 因为对于正整数,所有除法的通用定义都是相等的,所以它恰好等于 self.overflowing_div(rhs)

Panics

如果 rhs 为 0,则此函数将为 panic。

Examples

基本用法

assert_eq!(5u64.overflowing_div_euclid(2), (2, false));
Run

self 除以 rhs 时计算余数。

返回除法运算后的余数元组和一个布尔值,该布尔值指示是否会发生算术溢出。 请注意,对于无符号整数,永远不会发生溢出,因此第二个值始终为 false

Panics

如果 rhs 为 0,则此函数将为 panic。

Examples

基本用法

assert_eq!(5u64.overflowing_rem(2), (1, false));
Run

以欧几里得除法计算余数 self.rem_euclid(rhs)

返回除以布尔后的模元,并返回一个布尔值,指示是否会发生算术溢出。 请注意,对于无符号整数,永远不会发生溢出,因此第二个值始终为 false。 由于对于正整数,所有除法的通用定义均相等,因此该运算恰好等于 self.overflowing_rem(rhs)

Panics

如果 rhs 为 0,则此函数将为 panic。

Examples

基本用法

assert_eq!(5u64.overflowing_rem_euclid(2), (1, false));
Run

以一种泛滥的方式否定自我。

使用包装操作返回 !self + 1,以返回表示该无符号值的取反的值。 请注意,对于正的无符号值,总是会发生溢出,但取反 0 不会溢出。

Examples

基本用法

assert_eq!(0u64.overflowing_neg(), (0, false));
assert_eq!(2u64.overflowing_neg(), (-2i32 as u64, true));
Run

将自身左移 rhs 位。

返回 self 的移位版本的元组以及一个布尔值,该布尔值指示 shift 值是否大于或等于位数。 如果移位值太大,则将值屏蔽 (N-1),其中 N 是位数,然后使用该值执行移位。

Examples

基本用法

assert_eq!(0x1u64.overflowing_shl(4), (0x10, false));
assert_eq!(0x1u64.overflowing_shl(132), (0x10, true));
Run

将 self 右移 rhs 位。

返回 self 的移位版本的元组以及一个布尔值,该布尔值指示 shift 值是否大于或等于位数。 如果移位值太大,则将值屏蔽 (N-1),其中 N 是位数,然后使用该值执行移位。

Examples

基本用法

assert_eq!(0x10u64.overflowing_shr(4), (0x1, false));
assert_eq!(0x10u64.overflowing_shr(132), (0x1, true));
Run

通过平方运算,将自己提升到 exp 的功效。

返回一个指数的元组以及一个 bool,指示是否发生了溢出。

Examples

基本用法:

assert_eq!(3u64.overflowing_pow(5), (243, false));
assert_eq!(3u8.overflowing_pow(6), (217, true));
Run

通过平方运算,将自己提升到 exp 的功效。

Examples

基本用法:

assert_eq!(2u64.pow(5), 32);
Run

执行欧几里得除法。

因为对于正整数,所有除法的通用定义都是相等的,所以它恰好等于 self / rhs

Panics

如果 rhs 为 0,则此函数将为 panic。

Examples

基本用法:

assert_eq!(7u64.div_euclid(4), 1); // or any other integer type
Run

计算 self (mod rhs) 的最小余数。

因为对于正整数,所有除法的通用定义都是相等的,所以它恰好等于 self % rhs

Panics

如果 rhs 为 0,则此函数将为 panic。

Examples

基本用法:

assert_eq!(7u64.rem_euclid(4), 3); // or any other integer type
Run

当且仅当某些 kself == 2^k 时,才返回 true

Examples

基本用法:

assert!(16u64.is_power_of_two());
assert!(!10u64.is_power_of_two());
Run

返回大于或等于 self 的 2 的最小幂。

当返回值溢出时 (即 uN 类型为 self > (1 << (N-1))),它在调试模式下为 panics,在释放模式下返回值为 0 (这是唯一的方法可以返回 0 的情况)。

Examples

基本用法:

assert_eq!(2u64.next_power_of_two(), 2);
assert_eq!(3u64.next_power_of_two(), 4);
Run

返回大于或等于 n 的 2 的最小幂。 如果下一个 2 的幂大于该类型的最大值,则返回 None,否则将 2 的幂包装在 Some 中。

Examples

基本用法:

assert_eq!(2u64.checked_next_power_of_two(), Some(2));
assert_eq!(3u64.checked_next_power_of_two(), Some(4));
assert_eq!(u64::MAX.checked_next_power_of_two(), None);
Run
🔬 This is a nightly-only experimental API. (wrapping_next_power_of_two #32463)

needs decision on wrapping behaviour

返回大于或等于 n 的 2 的最小幂。 如果下一个 2 的幂大于该类型的最大值,则返回值将包装为 0

Examples

基本用法:

#![feature(wrapping_next_power_of_two)]

assert_eq!(2u64.wrapping_next_power_of_two(), 2);
assert_eq!(3u64.wrapping_next_power_of_two(), 4);
assert_eq!(u64::MAX.wrapping_next_power_of_two(), 0);
Run

以大端 (网络) 字节顺序将这个整数的内存表示形式作为字节数组返回。

Examples

let bytes = 0x1234567890123456u64.to_be_bytes();
assert_eq!(bytes, [0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]);
Run

以小端字节顺序将这个整数的内存表示形式返回为字节数组。

Examples

let bytes = 0x1234567890123456u64.to_le_bytes();
assert_eq!(bytes, [0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]);
Run

将此整数的内存表示作为本机字节顺序的字节数组返回。

由于使用了目标平台的原生字节序,因此,可移植代码应酌情使用 to_be_bytesto_le_bytes

Examples

let bytes = 0x1234567890123456u64.to_ne_bytes();
assert_eq!(
    bytes,
    if cfg!(target_endian = "big") {
        [0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]
    } else {
        [0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]
    }
);
Run

根据其表示形式 (大字节序中的字节数组) 创建一个本地字节序整数值。

Examples

let value = u64::from_be_bytes([0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]);
assert_eq!(value, 0x1234567890123456);
Run

从切片而不是数组开始时,可以使用容易出错的转换 API:

use std::convert::TryInto;

fn read_be_u64(input: &mut &[u8]) -> u64 {
    let (int_bytes, rest) = input.split_at(std::mem::size_of::<u64>());
    *input = rest;
    u64::from_be_bytes(int_bytes.try_into().unwrap())
}
Run

从它的表示形式以 little endian 的字节数组创建一个本地 endian 整数值。

Examples

let value = u64::from_le_bytes([0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]);
assert_eq!(value, 0x1234567890123456);
Run

从切片而不是数组开始时,可以使用容易出错的转换 API:

use std::convert::TryInto;

fn read_le_u64(input: &mut &[u8]) -> u64 {
    let (int_bytes, rest) = input.split_at(std::mem::size_of::<u64>());
    *input = rest;
    u64::from_le_bytes(int_bytes.try_into().unwrap())
}
Run

从其内存表示形式以原生字节序形式创建一个原生字节序整数值。

由于使用了目标平台的原生字节序,因此可移植代码可能希望酌情使用 from_be_bytesfrom_le_bytes

Examples

let value = u64::from_ne_bytes(if cfg!(target_endian = "big") {
    [0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]
} else {
    [0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]
});
assert_eq!(value, 0x1234567890123456);
Run

从切片而不是数组开始时,可以使用容易出错的转换 API:

use std::convert::TryInto;

fn read_ne_u64(input: &mut &[u8]) -> u64 {
    let (int_bytes, rest) = input.split_at(std::mem::size_of::<u64>());
    *input = rest;
    u64::from_ne_bytes(int_bytes.try_into().unwrap())
}
Run
👎 Deprecating in a future Rust version:

replaced by the MIN associated constant on this type

新代码应优先使用 u64::MIN instead.

返回此整数类型可以表示的最小值。

👎 Deprecating in a future Rust version:

replaced by the MAX associated constant on this type

新代码应优先使用 u64::MAX instead.

返回此整数类型可以表示的最大值。

Trait Implementations

应用 + 运算符后的结果类型。

执行 + 操作。 Read more

应用 + 运算符后的结果类型。

执行 + 操作。 Read more

应用 + 运算符后的结果类型。

执行 + 操作。 Read more

应用 + 运算符后的结果类型。

执行 + 操作。 Read more

执行 += 操作。 Read more

执行 += 操作。 Read more

使用给定的格式化程序格式化该值。

应用 & 运算符后的结果类型。

执行 & 操作。 Read more

应用 & 运算符后的结果类型。

执行 & 操作。 Read more

应用 & 运算符后的结果类型。

执行 & 操作。 Read more

应用 & 运算符后的结果类型。

执行 & 操作。 Read more

执行 &= 操作。 Read more

执行 &= 操作。 Read more

应用 | 运算符后的结果类型。

执行 | 操作。 Read more

应用 | 运算符后的结果类型。

执行 | 操作。 Read more

应用 | 运算符后的结果类型。

执行 | 操作。 Read more

应用 | 运算符后的结果类型。

执行 | 操作。 Read more

应用 | 运算符后的结果类型。

执行 | 操作。 Read more

执行 |= 操作。 Read more

执行 |= 操作。 Read more

应用 ^ 运算符后的结果类型。

执行 ^ 操作。 Read more

应用 ^ 运算符后的结果类型。

执行 ^ 操作。 Read more

应用 ^ 运算符后的结果类型。

执行 ^ 操作。 Read more

应用 ^ 运算符后的结果类型。

执行 ^ 操作。 Read more

执行 ^= 操作。 Read more

执行 ^= 操作。 Read more

返回值的副本。 Read more

source 执行复制分配。 Read more

使用给定的格式化程序格式化该值。 Read more

Returns the default value of 0

使用给定的格式化程序格式化该值。 Read more

应用 / 运算符后的结果类型。

执行 / 操作。 Read more

应用 / 运算符后的结果类型。

执行 / 操作。 Read more

此运算将舍入为零,舍去精确结果的任何小数部分,并且不能为 panic。

应用 / 运算符后的结果类型。

此运算将舍入为零,舍去精确结果的任何小数部分。

Panics

This operation will panic if other == 0.

应用 / 运算符后的结果类型。

执行 / 操作。 Read more

应用 / 运算符后的结果类型。

执行 / 操作。 Read more

执行 /= 操作。 Read more

执行 /= 操作。 Read more

Converts a NonZeroU64 into an u64

Converts a bool to a u64. The resulting value is 0 for false and 1 for true values.

Examples

assert_eq!(u64::from(true), 1);
assert_eq!(u64::from(false), 0);
Run

char 转换为 u64

Examples

use std::mem;

let c = '👤';
let u = u64::from(c);
assert!(8 == mem::size_of_val(&u))
Run

Converts u16 to u64 losslessly.

Converts u32 to u64 losslessly.

Converts u8 to u64 losslessly.

可以从解析中返回的相关错误。

解析字符串 s 以返回此类型的值。 Read more

将该值输入给定的 HasherRead more

将这种类型的切片送入给定的 Hasher 中。 Read more

使用给定的格式化程序格式化该值。

使用给定的格式化程序格式化该值。

应用 * 运算符后的结果类型。

执行 * 操作。 Read more

应用 * 运算符后的结果类型。

执行 * 操作。 Read more

应用 * 运算符后的结果类型。

执行 * 操作。 Read more

应用 * 运算符后的结果类型。

执行 * 操作。 Read more

执行 *= 操作。 Read more

执行 *= 操作。 Read more

应用 ! 运算符后的结果类型。

执行一元 ! 操作。 Read more

应用 ! 运算符后的结果类型。

执行一元 ! 操作。 Read more

使用给定的格式化程序格式化该值。

此方法返回 selfother 之间的 OrderingRead more

比较并返回两个值中的最大值。 Read more

比较并返回两个值中的最小值。 Read more

将值限制为一定的时间间隔。 Read more

此方法测试 selfother 值是否相等,并由 == 使用。 Read more

此方法测试 !=

如果存在,则此方法返回 selfother 值之间的顺序。 Read more

此方法测试的内容少于 (对于 selfother),并且由 < 操作员使用。 Read more

此方法测试小于或等于 (对于 selfother),并且由 <= 运算符使用。 Read more

此方法测试是否大于或等于 (对于 selfother),并且由 >= 运算符使用。 Read more

此方法测试大于 (对于 selfother),并且由 > 操作员使用。 Read more

该方法采用迭代器并通过乘以项从元素生成 SelfRead more

该方法采用迭代器并通过乘以项从元素生成 SelfRead more

应用 % 运算符后的结果类型。

执行 % 操作。 Read more

应用 % 运算符后的结果类型。

执行 % 操作。 Read more

此操作满足 n % d == n - (n / d) * d,但不能为 panic。

应用 % 运算符后的结果类型。

此操作满足 n % d == n - (n / d) * d。 结果具有与左操作数相同的符号。

Panics

This operation will panic if other == 0.

应用 % 运算符后的结果类型。

执行 % 操作。 Read more

应用 % 运算符后的结果类型。

执行 % 操作。 Read more

执行 %= 操作。 Read more

执行 %= 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

应用 << 运算符后的结果类型。

执行 << 操作。 Read more

执行 <<= 操作。 Read more

执行 <<= 操作。 Read more

执行 <<= 操作。 Read more

执行 <<= 操作。 Read more

执行 <<= 操作。 Read more

执行 <<= 操作。 Read more

执行 <<= 操作。 Read more

执行 <<= 操作。 Read more

执行 <<= 操作。 Read more

执行 <<= 操作。 Read more

执行 <<= 操作。 Read more

执行 <<= 操作。 Read more

执行 <<= 操作。 Read more

执行 <<= 操作。 Read more

执行 <<= 操作。 Read more

执行 <<= 操作。 Read more

执行 <<= 操作。 Read more

执行 <<= 操作。 Read more

执行 <<= 操作。 Read more

执行 <<= 操作。 Read more

执行 <<= 操作。 Read more

执行 <<= 操作。 Read more

执行 <<= 操作。 Read more

执行 <<= 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

应用 >> 运算符后的结果类型。

执行 >> 操作。 Read more

执行 >>= 操作。 Read more

执行 >>= 操作。 Read more

执行 >>= 操作。 Read more

执行 >>= 操作。 Read more

执行 >>= 操作。 Read more

执行 >>= 操作。 Read more

执行 >>= 操作。 Read more

执行 >>= 操作。 Read more

执行 >>= 操作。 Read more

执行 >>= 操作。 Read more

执行 >>= 操作。 Read more

执行 >>= 操作。 Read more

执行 >>= 操作。 Read more

执行 >>= 操作。 Read more

执行 >>= 操作。 Read more

执行 >>= 操作。 Read more

执行 >>= 操作。 Read more

执行 >>= 操作。 Read more

执行 >>= 操作。 Read more

执行 >>= 操作。 Read more

执行 >>= 操作。 Read more

执行 >>= 操作。 Read more

执行 >>= 操作。 Read more

执行 >>= 操作。 Read more

🔬 This is a nightly-only experimental API. (step_trait #42168)

recently redesigned

返回通过将 self countsuccessor 而获得的值。 Read more

🔬 This is a nightly-only experimental API. (step_trait #42168)

recently redesigned

返回通过获取 self count 次的 predecessor 而获得的值。 Read more

🔬 This is a nightly-only experimental API. (step_trait #42168)

recently redesigned

返回通过将 self countsuccessor 而获得的值。 Read more

🔬 This is a nightly-only experimental API. (step_trait #42168)

recently redesigned

返回通过获取 self count 次的 predecessor 而获得的值。 Read more

🔬 This is a nightly-only experimental API. (step_trait #42168)

recently redesigned

返回从 startend 所需的 successor 步骤的数量。 Read more

🔬 This is a nightly-only experimental API. (step_trait #42168)

recently redesigned

返回通过将 self countsuccessor 而获得的值。 Read more

🔬 This is a nightly-only experimental API. (step_trait #42168)

recently redesigned

返回通过获取 self count 次的 predecessor 而获得的值。 Read more

应用 - 运算符后的结果类型。

执行 - 操作。 Read more

应用 - 运算符后的结果类型。

执行 - 操作。 Read more

应用 - 运算符后的结果类型。

执行 - 操作。 Read more

应用 - 运算符后的结果类型。

执行 - 操作。 Read more

执行 -= 操作。 Read more

执行 -= 操作。 Read more

使用迭代器并通过 “summing up” 项从元素生成 Self 的方法。 Read more

使用迭代器并通过 “summing up” 项从元素生成 Self 的方法。 Read more

尝试从源号码类型创建目标号码类型。 如果源值超出目标类型的范围,则返回错误。

发生转换错误时返回的类型。

尝试从源号码类型创建目标号码类型。 如果源值超出目标类型的范围,则返回错误。

发生转换错误时返回的类型。

尝试从源号码类型创建目标号码类型。 如果源值超出目标类型的范围,则返回错误。

发生转换错误时返回的类型。

尝试从源号码类型创建目标号码类型。 如果源值超出目标类型的范围,则返回错误。

发生转换错误时返回的类型。

尝试从源号码类型创建目标号码类型。 如果源值超出目标类型的范围,则返回错误。

发生转换错误时返回的类型。

尝试从源号码类型创建目标号码类型。 如果源值超出目标类型的范围,则返回错误。

发生转换错误时返回的类型。

尝试从源号码类型创建目标号码类型。 如果源值超出目标类型的范围,则返回错误。

发生转换错误时返回的类型。

尝试从源号码类型创建目标号码类型。 如果源值超出目标类型的范围,则返回错误。

发生转换错误时返回的类型。

使用给定的格式化程序格式化该值。

使用给定的格式化程序格式化该值。

Auto Trait Implementations

Blanket Implementations

获取 selfTypeIdRead more

从拥有的值中一成不变地借用。 Read more

从拥有的值中借用。 Read more

执行转换。

执行转换。

获得所有权后的结果类型。

通常通过克隆从借用数据中创建拥有的数据。 Read more

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

recently added

使用借来的数据来替换拥有的数据,通常是通过克隆。 Read more

将给定值转换为 StringRead more

发生转换错误时返回的类型。

执行转换。

发生转换错误时返回的类型。

执行转换。