Struct core::num::NonZeroUsize

1.28.0 · source ·
#[repr(transparent)]
pub struct NonZeroUsize(_);
Expand description

已知不等于零的整数。

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

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

Layout

NonZeroUsize is guaranteed to have the same layout and bit validity as usize 除了 0 不是有效实例。 Option<NonZeroUsize> is guaranteed to be compatible with usize, 包括在 FFI 中。

Implementations§

source§

impl NonZeroUsize

const: 1.28.0 · source

pub const unsafe fn new_unchecked(n: usize) -> Self

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

Safety

该值不能为零。

const: 1.47.0 · source

pub const fn new(n: usize) -> Option<Self>

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

const: 1.34.0 · source

pub const fn get(self) -> usize

将值作为原始类型返回。

source§

impl NonZeroUsize

1.53.0 (const: 1.53.0) · source

pub const fn leading_zeros(self) -> u32

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

在许多体系结构上,此函数在底层整数类型上的性能优于 leading_zeros(),因为可以避免对零的特殊处理。

Examples

基本用法:

let n = std::num::NonZeroUsize::new(usize::MAX).unwrap();

assert_eq!(n.leading_zeros(), 0);
Run
1.53.0 (const: 1.53.0) · source

pub const fn trailing_zeros(self) -> u32

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

在许多体系结构上,此函数在底层整数类型上的性能优于 trailing_zeros(),因为可以避免对零的特殊处理。

Examples

基本用法:

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

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

impl NonZeroUsize

1.64.0 (const: 1.64.0) · source

pub const fn checked_add(self, other: usize) -> Option<NonZeroUsize>

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

Examples
let one = NonZeroUsize::new(1)?;
let two = NonZeroUsize::new(2)?;
let max = NonZeroUsize::new(usize::MAX)?;

assert_eq!(Some(two), one.checked_add(1));
assert_eq!(None, max.checked_add(1));
Run
1.64.0 (const: 1.64.0) · source

pub const fn saturating_add(self, other: usize) -> NonZeroUsize

将无符号整数与非零值相加。 Return usize::MAX on overflow.

Examples
let one = NonZeroUsize::new(1)?;
let two = NonZeroUsize::new(2)?;
let max = NonZeroUsize::new(usize::MAX)?;

assert_eq!(two, one.saturating_add(1));
assert_eq!(max, max.saturating_add(1));
Run
source

pub const unsafe fn unchecked_add(self, other: usize) -> NonZeroUsize

🔬This is a nightly-only experimental API. (nonzero_ops #84186)

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

该行为是未定义的

self + rhs > usize::MAX.

Examples
#![feature(nonzero_ops)]

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

assert_eq!(two, unsafe { one.unchecked_add(1) });
Run
1.64.0 (const: 1.64.0) · source

pub const fn checked_next_power_of_two(self) -> Option<NonZeroUsize>

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

Examples


let two = NonZeroUsize::new(2)?;
let three = NonZeroUsize::new(3)?;
let four = NonZeroUsize::new(4)?;
let max = NonZeroUsize::new(usize::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
1.67.0 (const: 1.67.0) · source

pub const fn ilog2(self) -> u32

返回数字的以 2 为底的对数,向下取整。

这是相同的操作 usize::ilog2, 除了它没有需要担心的失败案例,因为这个值永远不会为零。

Examples
assert_eq!(NonZeroUsize::new(7).unwrap().ilog2(), 2);
assert_eq!(NonZeroUsize::new(8).unwrap().ilog2(), 3);
assert_eq!(NonZeroUsize::new(9).unwrap().ilog2(), 3);
Run
1.67.0 (const: 1.67.0) · source

pub const fn ilog10(self) -> u32

返回数字的以 10 为底的对数,向下取整。

这是相同的操作 usize::ilog10, 除了它没有需要担心的失败案例,因为这个值永远不会为零。

Examples
assert_eq!(NonZeroUsize::new(99).unwrap().ilog10(), 1);
assert_eq!(NonZeroUsize::new(100).unwrap().ilog10(), 2);
assert_eq!(NonZeroUsize::new(101).unwrap().ilog10(), 2);
Run
const: unstable · source

pub fn midpoint(self, rhs: Self) -> Self

🔬This is a nightly-only experimental API. (num_midpoint #110840)

计算 selfrhs 的中点。

midpoint(a, b)(a + b) >> 1,就好像它是在足够大的带符号整数类型中执行的一样。 这意味着结果总是向 negative 无穷大舍入,并且永远不会发生溢出。

Examples
#![feature(num_midpoint)]

let one = NonZeroUsize::new(1)?;
let two = NonZeroUsize::new(2)?;
let four = NonZeroUsize::new(4)?;

assert_eq!(one.midpoint(four), two);
assert_eq!(four.midpoint(one), two);
Run
source§

impl NonZeroUsize

1.64.0 (const: 1.64.0) · source

pub const fn checked_mul(self, other: NonZeroUsize) -> Option<NonZeroUsize>

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

Examples
let two = NonZeroUsize::new(2)?;
let four = NonZeroUsize::new(4)?;
let max = NonZeroUsize::new(usize::MAX)?;

assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));
Run
1.64.0 (const: 1.64.0) · source

pub const fn saturating_mul(self, other: NonZeroUsize) -> NonZeroUsize

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

Examples
let two = NonZeroUsize::new(2)?;
let four = NonZeroUsize::new(4)?;
let max = NonZeroUsize::new(usize::MAX)?;

assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));
Run
source

pub const unsafe fn unchecked_mul(self, other: NonZeroUsize) -> NonZeroUsize

🔬This is a nightly-only experimental API. (nonzero_ops #84186)

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

该行为是未定义的

self * rhs > usize::MAX.

Examples
#![feature(nonzero_ops)]

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

assert_eq!(four, unsafe { two.unchecked_mul(two) });
Run
1.64.0 (const: 1.64.0) · source

pub const fn checked_pow(self, other: u32) -> Option<NonZeroUsize>

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

Examples
let three = NonZeroUsize::new(3)?;
let twenty_seven = NonZeroUsize::new(27)?;
let half_max = NonZeroUsize::new(usize::MAX / 2)?;

assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));
Run
1.64.0 (const: 1.64.0) · source

pub const fn saturating_pow(self, other: u32) -> NonZeroUsize

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

Examples
let three = NonZeroUsize::new(3)?;
let twenty_seven = NonZeroUsize::new(27)?;
let max = NonZeroUsize::new(usize::MAX)?;

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

impl NonZeroUsize

1.59.0 (const: 1.59.0) · source

pub const fn is_power_of_two(self) -> bool

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

在许多体系结构上,此函数在底层整数类型上的性能优于 is_power_of_two(),因为可以避免对零的特殊处理。

Examples

基本用法:

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

impl NonZeroUsize

1.70.0 · source

pub const MIN: Self = _

这个非零整数类型可以表示的最小值, 1.

Examples
assert_eq!(NonZeroUsize::MIN.get(), 1usize);
Run
1.70.0 · source

pub const MAX: Self = _

这个非零整数类型可以表示的最大值,

equal to usize::MAX.

Examples
assert_eq!(NonZeroUsize::MAX.get(), usize::MAX);
Run
source§

impl NonZeroUsize

1.67.0 · source

pub const BITS: u32 = 64u32

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

This value is equal to usize::BITS.

Examples

assert_eq!(NonZeroUsize::BITS, usize::BITS);
Run

Trait Implementations§

source§

impl Binary for NonZeroUsize

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

使用给定的格式化程序格式化该值。
1.45.0 · source§

impl BitOr<NonZeroUsize> for NonZeroUsize

§

type Output = NonZeroUsize

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

fn bitor(self, rhs: Self) -> Self::Output

执行 | 操作。 Read more
1.45.0 · source§

impl BitOr<NonZeroUsize> for usize

§

type Output = NonZeroUsize

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

fn bitor(self, rhs: NonZeroUsize) -> Self::Output

执行 | 操作。 Read more
1.45.0 · source§

impl BitOr<usize> for NonZeroUsize

§

type Output = NonZeroUsize

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

fn bitor(self, rhs: usize) -> Self::Output

执行 | 操作。 Read more
1.45.0 · source§

impl BitOrAssign<NonZeroUsize> for NonZeroUsize

source§

fn bitor_assign(&mut self, rhs: Self)

执行 |= 操作。 Read more
1.45.0 · source§

impl BitOrAssign<usize> for NonZeroUsize

source§

fn bitor_assign(&mut self, rhs: usize)

执行 |= 操作。 Read more
source§

impl Clone for NonZeroUsize

source§

fn clone(&self) -> NonZeroUsize

返回值的副本。 Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

source 执行复制分配。 Read more
source§

impl Debug for NonZeroUsize

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

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

impl Display for NonZeroUsize

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

使用给定的格式化程序格式化该值。 Read more
1.51.0 · source§

impl Div<NonZeroUsize> for usize

source§

fn div(self, other: NonZeroUsize) -> usize

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

§

type Output = usize

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

impl From<Alignment> for NonZeroUsize

source§

fn from(align: Alignment) -> NonZeroUsize

从输入类型转换为此类型。
1.41.0 · source§

impl From<NonZeroU16> for NonZeroUsize

source§

fn from(small: NonZeroU16) -> Self

Converts NonZeroU16 to NonZeroUsize losslessly.

1.41.0 · source§

impl From<NonZeroU8> for NonZeroUsize

source§

fn from(small: NonZeroU8) -> Self

Converts NonZeroU8 to NonZeroUsize losslessly.

1.31.0 · source§

impl From<NonZeroUsize> for usize

source§

fn from(nonzero: NonZeroUsize) -> Self

Converts a NonZeroUsize into an usize

1.35.0 · source§

impl FromStr for NonZeroUsize

§

type Err = ParseIntError

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

fn from_str(src: &str) -> Result<Self, Self::Err>

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

impl Hash for NonZeroUsize

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

将该值输入给定的 HasherRead more
1.3.0 · source§

fn hash_slice<H: Hasher>(data: &[Self], state: &mut H)where Self: Sized,

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

impl LowerHex for NonZeroUsize

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

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

impl Octal for NonZeroUsize

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

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

impl Ord for NonZeroUsize

source§

fn cmp(&self, other: &NonZeroUsize) -> Ordering

此方法返回 selfother 之间的 OrderingRead more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

比较并返回两个值中的最大值。 Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

比较并返回两个值中的最小值。 Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd,

将值限制在某个时间间隔内。 Read more
source§

impl PartialEq<NonZeroUsize> for NonZeroUsize

source§

fn eq(&self, other: &NonZeroUsize) -> bool

此方法测试 selfother 值是否相等,并由 == 使用。
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

此方法测试 !=。 默认实现几乎总是足够的,并且不应在没有充分理由的情况下被覆盖。
source§

impl PartialOrd<NonZeroUsize> for NonZeroUsize

source§

fn partial_cmp(&self, other: &NonZeroUsize) -> Option<Ordering>

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

fn lt(&self, other: &Rhs) -> bool

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

fn le(&self, other: &Rhs) -> bool

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

fn gt(&self, other: &Rhs) -> bool

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

fn ge(&self, other: &Rhs) -> bool

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

impl Rem<NonZeroUsize> for usize

source§

fn rem(self, other: NonZeroUsize) -> usize

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

§

type Output = usize

应用 % 运算符后的结果类型。
1.49.0 · source§

impl TryFrom<NonZeroI128> for NonZeroUsize

source§

fn try_from(value: NonZeroI128) -> Result<Self, Self::Error>

Attempts to convert NonZeroI128 to NonZeroUsize.

§

type Error = TryFromIntError

发生转换错误时返回的类型。
1.49.0 · source§

impl TryFrom<NonZeroI16> for NonZeroUsize

source§

fn try_from(value: NonZeroI16) -> Result<Self, Self::Error>

Attempts to convert NonZeroI16 to NonZeroUsize.

§

type Error = TryFromIntError

发生转换错误时返回的类型。
1.49.0 · source§

impl TryFrom<NonZeroI32> for NonZeroUsize

source§

fn try_from(value: NonZeroI32) -> Result<Self, Self::Error>

Attempts to convert NonZeroI32 to NonZeroUsize.

§

type Error = TryFromIntError

发生转换错误时返回的类型。
1.49.0 · source§

impl TryFrom<NonZeroI64> for NonZeroUsize

source§

fn try_from(value: NonZeroI64) -> Result<Self, Self::Error>

Attempts to convert NonZeroI64 to NonZeroUsize.

§

type Error = TryFromIntError

发生转换错误时返回的类型。
1.49.0 · source§

impl TryFrom<NonZeroI8> for NonZeroUsize

source§

fn try_from(value: NonZeroI8) -> Result<Self, Self::Error>

Attempts to convert NonZeroI8 to NonZeroUsize.

§

type Error = TryFromIntError

发生转换错误时返回的类型。
1.49.0 · source§

impl TryFrom<NonZeroIsize> for NonZeroUsize

source§

fn try_from(value: NonZeroIsize) -> Result<Self, Self::Error>

Attempts to convert NonZeroIsize to NonZeroUsize.

§

type Error = TryFromIntError

发生转换错误时返回的类型。
1.49.0 · source§

impl TryFrom<NonZeroU128> for NonZeroUsize

source§

fn try_from(value: NonZeroU128) -> Result<Self, Self::Error>

Attempts to convert NonZeroU128 to NonZeroUsize.

§

type Error = TryFromIntError

发生转换错误时返回的类型。
1.49.0 · source§

impl TryFrom<NonZeroU32> for NonZeroUsize

source§

fn try_from(value: NonZeroU32) -> Result<Self, Self::Error>

Attempts to convert NonZeroU32 to NonZeroUsize.

§

type Error = TryFromIntError

发生转换错误时返回的类型。
1.49.0 · source§

impl TryFrom<NonZeroU64> for NonZeroUsize

source§

fn try_from(value: NonZeroU64) -> Result<Self, Self::Error>

Attempts to convert NonZeroU64 to NonZeroUsize.

§

type Error = TryFromIntError

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

impl TryFrom<NonZeroUsize> for Alignment

§

type Error = TryFromIntError

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

fn try_from(align: NonZeroUsize) -> Result<Alignment, Self::Error>

执行转换。
1.49.0 · source§

impl TryFrom<NonZeroUsize> for NonZeroI128

source§

fn try_from(value: NonZeroUsize) -> Result<Self, Self::Error>

Attempts to convert NonZeroUsize to NonZeroI128.

§

type Error = TryFromIntError

发生转换错误时返回的类型。
1.49.0 · source§

impl TryFrom<NonZeroUsize> for NonZeroI16

source§

fn try_from(value: NonZeroUsize) -> Result<Self, Self::Error>

Attempts to convert NonZeroUsize to NonZeroI16.

§

type Error = TryFromIntError

发生转换错误时返回的类型。
1.49.0 · source§

impl TryFrom<NonZeroUsize> for NonZeroI32

source§

fn try_from(value: NonZeroUsize) -> Result<Self, Self::Error>

Attempts to convert NonZeroUsize to NonZeroI32.

§

type Error = TryFromIntError

发生转换错误时返回的类型。
1.49.0 · source§

impl TryFrom<NonZeroUsize> for NonZeroI64

source§

fn try_from(value: NonZeroUsize) -> Result<Self, Self::Error>

Attempts to convert NonZeroUsize to NonZeroI64.

§

type Error = TryFromIntError

发生转换错误时返回的类型。
1.49.0 · source§

impl TryFrom<NonZeroUsize> for NonZeroI8

source§

fn try_from(value: NonZeroUsize) -> Result<Self, Self::Error>

Attempts to convert NonZeroUsize to NonZeroI8.

§

type Error = TryFromIntError

发生转换错误时返回的类型。
1.49.0 · source§

impl TryFrom<NonZeroUsize> for NonZeroIsize

source§

fn try_from(value: NonZeroUsize) -> Result<Self, Self::Error>

Attempts to convert NonZeroUsize to NonZeroIsize.

§

type Error = TryFromIntError

发生转换错误时返回的类型。
1.49.0 · source§

impl TryFrom<NonZeroUsize> for NonZeroU128

source§

fn try_from(value: NonZeroUsize) -> Result<Self, Self::Error>

Attempts to convert NonZeroUsize to NonZeroU128.

§

type Error = TryFromIntError

发生转换错误时返回的类型。
1.49.0 · source§

impl TryFrom<NonZeroUsize> for NonZeroU16

source§

fn try_from(value: NonZeroUsize) -> Result<Self, Self::Error>

Attempts to convert NonZeroUsize to NonZeroU16.

§

type Error = TryFromIntError

发生转换错误时返回的类型。
1.49.0 · source§

impl TryFrom<NonZeroUsize> for NonZeroU32

source§

fn try_from(value: NonZeroUsize) -> Result<Self, Self::Error>

Attempts to convert NonZeroUsize to NonZeroU32.

§

type Error = TryFromIntError

发生转换错误时返回的类型。
1.49.0 · source§

impl TryFrom<NonZeroUsize> for NonZeroU64

source§

fn try_from(value: NonZeroUsize) -> Result<Self, Self::Error>

Attempts to convert NonZeroUsize to NonZeroU64.

§

type Error = TryFromIntError

发生转换错误时返回的类型。
1.49.0 · source§

impl TryFrom<NonZeroUsize> for NonZeroU8

source§

fn try_from(value: NonZeroUsize) -> Result<Self, Self::Error>

Attempts to convert NonZeroUsize to NonZeroU8.

§

type Error = TryFromIntError

发生转换错误时返回的类型。
1.46.0 · source§

impl TryFrom<usize> for NonZeroUsize

source§

fn try_from(value: usize) -> Result<Self, Self::Error>

Attempts to convert usize to NonZeroUsize.

§

type Error = TryFromIntError

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

impl UpperHex for NonZeroUsize

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

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

impl Copy for NonZeroUsize

source§

impl Eq for NonZeroUsize

source§

impl StructuralEq for NonZeroUsize

source§

impl StructuralPartialEq for NonZeroUsize

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

获取 selfTypeIdRead more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

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

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

从拥有的值中借用。 Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

返回未更改的参数。

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

调用 U::from(self)

也就是说,这种转换是 From<T> for U 实现选择执行的任何操作。

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

执行转换。
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

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

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

执行转换。