Struct std::num::NonZeroI321.34.0[][src]

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

已知不等于零的整数。

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

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

Implementations

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

Safety

该值不能为零。

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

以原始类型返回该值。

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

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

Examples

基本用法:

let n = std::num::NonZeroI32::new(-1i32).unwrap();

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

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

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

Examples

基本用法:

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

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

计算 self 的绝对值。 See i32::abs 有关溢出行为的文档。

Example

#![feature(nonzero_ops)]

let pos = NonZeroI32::new(1)?;
let neg = NonZeroI32::new(-1)?;

assert_eq!(pos, pos.abs());
assert_eq!(pos, neg.abs());
Run
🔬 This is a nightly-only experimental API. (nonzero_ops #84186)

检查的绝对值。 检查溢出并返回 None 如果 self == i32::MIN. 结果不能为零。

Example

#![feature(nonzero_ops)]

let pos = NonZeroI32::new(1)?;
let neg = NonZeroI32::new(-1)?;
let min = NonZeroI32::new(i32::MIN)?;

assert_eq!(Some(pos), neg.checked_abs());
assert_eq!(None, min.checked_abs());
Run
🔬 This is a nightly-only experimental API. (nonzero_ops #84186)

计算 self 的绝对值,带有溢出信息,请参见

i32::overflowing_abs.

Example

#![feature(nonzero_ops)]

let pos = NonZeroI32::new(1)?;
let neg = NonZeroI32::new(-1)?;
let min = NonZeroI32::new(i32::MIN)?;

assert_eq!((pos, false), pos.overflowing_abs());
assert_eq!((pos, false), neg.overflowing_abs());
assert_eq!((min, true), min.overflowing_abs());
Run
🔬 This is a nightly-only experimental API. (nonzero_ops #84186)

饱和绝对值,请参见 i32::saturating_abs.

Example

#![feature(nonzero_ops)]

let pos = NonZeroI32::new(1)?;
let neg = NonZeroI32::new(-1)?;
let min = NonZeroI32::new(i32::MIN)?;
let min_plus = NonZeroI32::new(i32::MIN + 1)?;
let max = NonZeroI32::new(i32::MAX)?;

assert_eq!(pos, pos.saturating_abs());
assert_eq!(pos, neg.saturating_abs());
assert_eq!(max, min.saturating_abs());
assert_eq!(max, min_plus.saturating_abs());
Run
🔬 This is a nightly-only experimental API. (nonzero_ops #84186)

包装绝对值,请参见 i32::wrapping_abs.

Example

#![feature(nonzero_ops)]

let pos = NonZeroI32::new(1)?;
let neg = NonZeroI32::new(-1)?;
let min = NonZeroI32::new(i32::MIN)?;
let max = NonZeroI32::new(i32::MAX)?;

assert_eq!(pos, pos.wrapping_abs());
assert_eq!(pos, neg.wrapping_abs());
assert_eq!(min, min.wrapping_abs());
Run
🔬 This is a nightly-only experimental API. (nonzero_ops #84186)

在没有任何包装或 panicking 的情况下计算 self 的绝对值。

Example

#![feature(nonzero_ops)]


let u_pos = NonZeroU32::new(1)?;
let i_pos = NonZeroI32::new(1)?;
let i_neg = NonZeroI32::new(-1)?;
let i_min = NonZeroI32::new(i32::MIN)?;
let u_max = NonZeroU32::new(u32::MAX / 2 + 1)?;

assert_eq!(u_pos, i_pos.unsigned_abs());
assert_eq!(u_pos, i_neg.unsigned_abs());
assert_eq!(u_max, i_min.unsigned_abs());
Run
🔬 This is a nightly-only experimental API. (nonzero_ops #84186)

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

Examples

#![feature(nonzero_ops)]

let two = NonZeroI32::new(2)?;
let four = NonZeroI32::new(4)?;
let max = NonZeroI32::new(i32::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 i32::MAX on overflow.

Examples

#![feature(nonzero_ops)]

let two = NonZeroI32::new(2)?;
let four = NonZeroI32::new(4)?;
let max = NonZeroI32::new(i32::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 > i32::MAX, or self * rhs < i32::MIN.

Examples

#![feature(nonzero_ops)]

let two = NonZeroI32::new(2)?;
let four = NonZeroI32::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 = NonZeroI32::new(3)?;
let twenty_seven = NonZeroI32::new(27)?;
let half_max = NonZeroI32::new(i32::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 i32::MIN or i32::MAX on overflow.

Examples

#![feature(nonzero_ops)]

let three = NonZeroI32::new(3)?;
let twenty_seven = NonZeroI32::new(27)?;
let max = NonZeroI32::new(i32::MAX)?;

assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));
Run

Trait Implementations

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

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

执行 | 操作。 Read more

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

执行 | 操作。 Read more

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

执行 | 操作。 Read more

执行 |= 操作。 Read more

执行 |= 操作。 Read more

返回值的副本。 Read more

source 执行复制分配。 Read more

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

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

Converts NonZeroI16 to NonZeroI32 losslessly.

Converts NonZeroI32 to NonZeroI128 losslessly.

Converts NonZeroI32 to NonZeroI64 losslessly.

Converts a NonZeroI32 into an i32

Converts NonZeroI8 to NonZeroI32 losslessly.

Converts NonZeroU16 to NonZeroI32 losslessly.

Converts NonZeroU8 to NonZeroI32 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

Attempts to convert NonZeroI128 to NonZeroI32.

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

Attempts to convert NonZeroI32 to NonZeroI8.

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

Attempts to convert NonZeroI32 to NonZeroU128.

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

Attempts to convert NonZeroI32 to NonZeroI16.

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

Attempts to convert NonZeroI32 to NonZeroUsize.

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

Attempts to convert NonZeroI32 to NonZeroU32.

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

Attempts to convert NonZeroI32 to NonZeroU64.

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

Attempts to convert NonZeroI32 to NonZeroU8.

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

Attempts to convert NonZeroI32 to NonZeroIsize.

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

Attempts to convert NonZeroI32 to NonZeroU16.

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

Attempts to convert NonZeroI64 to NonZeroI32.

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

Attempts to convert NonZeroIsize to NonZeroI32.

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

Attempts to convert NonZeroU128 to NonZeroI32.

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

Attempts to convert NonZeroU32 to NonZeroI32.

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

Attempts to convert NonZeroU64 to NonZeroI32.

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

Attempts to convert NonZeroUsize to NonZeroI32.

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

Attempts to convert i32 to NonZeroI32.

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

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

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

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

执行转换。

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

执行转换。