Struct std::num::NonZeroU161.28.0[][src]

#[repr(transparent)]
pub struct NonZeroU16(_);
Expand description

已知不等于零的整数。

这样可以进行一些内存布局优化。 For example, Option<NonZeroU16> is the same size as u16:

use std::mem::size_of;
assert_eq!(size_of::<Option<core::num::NonZeroU16>>(), size_of::<u16>());
Run

Implementations

创建一个非零值而不检查该值是否为非零值。 如果该值为零,这将导致未定义的行为。

Safety

该值不能为零。

如果给定值不为零,则创建一个非零值。

以原始类型返回该值。

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

在许多体系结构上,此函数可以在基础整数类型上比 leading_zeros() 更好地执行,因为可以避免对零的特殊处理。

Examples

基本用法:

let n = std::num::NonZeroU16::new(u16::MAX).unwrap();

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

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

在许多体系结构上,此函数可以在基础整数类型上比 trailing_zeros() 更好地执行,因为可以避免对零的特殊处理。

Examples

基本用法:

let n = std::num::NonZeroU16::new(0b0101000).unwrap();

assert_eq!(n.trailing_zeros(), 3);
Run
🔬 This is a nightly-only experimental API. (nonzero_ops #84186)

将无符号整数添加到非零值。 检查溢出并在溢出时返回 None 因此,结果不能归零。

Examples

#![feature(nonzero_ops)]


let one = NonZeroU16::new(1)?;
let two = NonZeroU16::new(2)?;
let max = NonZeroU16::new(u16::MAX)?;

assert_eq!(Some(two), one.checked_add(1));
assert_eq!(None, max.checked_add(1));
Run
🔬 This is a nightly-only experimental API. (nonzero_ops #84186)

将无符号整数添加到非零值。 Return u16::MAX on overflow.

Examples

#![feature(nonzero_ops)]

let one = NonZeroU16::new(1)?;
let two = NonZeroU16::new(2)?;
let max = NonZeroU16::new(u16::MAX)?;

assert_eq!(two, one.saturating_add(1));
assert_eq!(max, max.saturating_add(1));
Run
🔬 This is a nightly-only experimental API. (nonzero_ops #84186)

假设不会发生溢出,将无符号整数添加到非零值。 溢出未检查,如果结果将换行为非零值,则溢出 *even 是未定义的行为 *。

该行为是未定义的

self + rhs > u16::MAX.

Examples

#![feature(nonzero_ops)]

let one = NonZeroU16::new(1)?;
let two = NonZeroU16::new(2)?;

assert_eq!(two, unsafe { one.unchecked_add(1) });
Run
🔬 This is a nightly-only experimental API. (nonzero_ops #84186)

返回大于或等于 n 的 2 的最小幂。 如果下一个 2 的幂大于类型的最大值,则检查是否溢出并返回 None。 因此,结果不能归零。

Examples

#![feature(nonzero_ops)]


let two = NonZeroU16::new(2)?;
let three = NonZeroU16::new(3)?;
let four = NonZeroU16::new(4)?;
let max = NonZeroU16::new(u16::MAX)?;

assert_eq!(Some(two), two.checked_next_power_of_two() );
assert_eq!(Some(four), three.checked_next_power_of_two() );
assert_eq!(None, max.checked_next_power_of_two() );
Run
🔬 This is a nightly-only experimental API. (nonzero_ops #84186)

将两个非零整数相乘。 检查溢出并在溢出时返回 None。 因此,结果不能归零。

Examples

#![feature(nonzero_ops)]

let two = NonZeroU16::new(2)?;
let four = NonZeroU16::new(4)?;
let max = NonZeroU16::new(u16::MAX)?;

assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));
Run
🔬 This is a nightly-only experimental API. (nonzero_ops #84186)

将两个非零整数相乘。 Return u16::MAX on overflow.

Examples

#![feature(nonzero_ops)]

let two = NonZeroU16::new(2)?;
let four = NonZeroU16::new(4)?;
let max = NonZeroU16::new(u16::MAX)?;

assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));
Run
🔬 This is a nightly-only experimental API. (nonzero_ops #84186)

假设不会发生溢出,将两个非零整数相乘。 溢出未检查,如果结果将换行为非零值,则溢出 *even 是未定义的行为 *。

该行为是未定义的

self * rhs > u16::MAX.

Examples

#![feature(nonzero_ops)]

let two = NonZeroU16::new(2)?;
let four = NonZeroU16::new(4)?;

assert_eq!(four, unsafe { two.unchecked_mul(two) });
Run
🔬 This is a nightly-only experimental API. (nonzero_ops #84186)

将非零值提高到整数幂。 检查溢出并在溢出时返回 None。 因此,结果不能归零。

Examples

#![feature(nonzero_ops)]

let three = NonZeroU16::new(3)?;
let twenty_seven = NonZeroU16::new(27)?;
let half_max = NonZeroU16::new(u16::MAX / 2)?;

assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));
Run
🔬 This is a nightly-only experimental API. (nonzero_ops #84186)

将非零值提高到整数幂。 Return u16::MAX on overflow.

Examples

#![feature(nonzero_ops)]

let three = NonZeroU16::new(3)?;
let twenty_seven = NonZeroU16::new(27)?;
let max = NonZeroU16::new(u16::MAX)?;

assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));
Run
🔬 This is a nightly-only experimental API. (nonzero_is_power_of_two #81106)

当且仅当某些 kself == (1 << k) 时,才返回 true

在许多体系结构上,此函数可以在基础整数类型上比 is_power_of_two() 更好地执行,因为可以避免对零的特殊处理。

Examples

基本用法:

#![feature(nonzero_is_power_of_two)]

let eight = std::num::NonZeroU16::new(8).unwrap();
assert!(eight.is_power_of_two());
let ten = std::num::NonZeroU16::new(10).unwrap();
assert!(!ten.is_power_of_two());
Run

Trait Implementations

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

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

执行 | 操作。 Read more

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

执行 | 操作。 Read more

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

执行 | 操作。 Read more

执行 |= 操作。 Read more

执行 |= 操作。 Read more

返回值的副本。 Read more

source 执行复制分配。 Read more

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

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

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

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

Converts NonZeroU16 to NonZeroI128 losslessly.

Converts NonZeroU16 to NonZeroI64 losslessly.

Converts NonZeroU16 to NonZeroU64 losslessly.

Converts NonZeroU16 to NonZeroU128 losslessly.

Converts a NonZeroU16 into an u16

Converts NonZeroU16 to NonZeroUsize losslessly.

Converts NonZeroU16 to NonZeroI32 losslessly.

Converts NonZeroU16 to NonZeroU32 losslessly.

Converts NonZeroU8 to NonZeroU16 losslessly.

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

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

将该值输入给定的 HasherRead more

将这种类型的切片送入给定的 Hasher 中。 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

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

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

Attempts to convert NonZeroI128 to NonZeroU16.

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

Attempts to convert NonZeroI16 to NonZeroU16.

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

Attempts to convert NonZeroI32 to NonZeroU16.

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

Attempts to convert NonZeroI64 to NonZeroU16.

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

Attempts to convert NonZeroI8 to NonZeroU16.

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

Attempts to convert NonZeroIsize to NonZeroU16.

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

Attempts to convert NonZeroU128 to NonZeroU16.

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

Attempts to convert NonZeroU16 to NonZeroI8.

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

Attempts to convert NonZeroU16 to NonZeroI16.

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

Attempts to convert NonZeroU16 to NonZeroIsize.

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

Attempts to convert NonZeroU16 to NonZeroU8.

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

Attempts to convert NonZeroU32 to NonZeroU16.

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

Attempts to convert NonZeroU64 to NonZeroU16.

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

Attempts to convert NonZeroUsize to NonZeroU16.

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

Attempts to convert u16 to NonZeroU16.

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

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

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

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

执行转换。

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

执行转换。