Struct core::num::NonZeroUsize 1.28.0[−][src]
#[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
Implementations
将无符号整数添加到非零值。
Return usize::MAX on overflow.
Examples
#![feature(nonzero_ops)] 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
返回大于或等于 n 的 2 的最小幂。
如果下一个 2 的幂大于类型的最大值,则检查是否溢出并返回 None。
因此,结果不能归零。
Examples
#![feature(nonzero_ops)] 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
将两个非零整数相乘。
Return usize::MAX on overflow.
Examples
#![feature(nonzero_ops)] 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
将非零值提高到整数幂。
检查溢出并在溢出时返回 None。
因此,结果不能归零。
Examples
#![feature(nonzero_ops)] 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
将非零值提高到整数幂。
Return usize::MAX on overflow.
Examples
#![feature(nonzero_ops)] 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
当且仅当某些 k 的 self == (1 << k) 时,才返回 true。
在许多体系结构上,此函数可以在基础整数类型上比 is_power_of_two() 更好地执行,因为可以避免对零的特殊处理。
Examples
基本用法:
#![feature(nonzero_is_power_of_two)] 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
Trait Implementations
type Output = NonZeroUsize
type Output = NonZeroUsize
应用 | 运算符后的结果类型。
执行 | 操作。 Read more
执行 |= 操作。 Read more
执行 |= 操作。 Read more
此运算将舍入为零,舍去精确结果的任何小数部分,并且不能为 panic。
type Output = usize
type Output = usize
应用 / 运算符后的结果类型。
Converts NonZeroU16 to NonZeroUsize losslessly.
Converts a NonZeroUsize into an usize
此方法测试 self 和 other 值是否相等,并由 == 使用。 Read more
此方法测试 !=。
此操作满足 n % d == n - (n / d) * d,但不能为 panic。
type Output = usize
type Output = usize
应用 % 运算符后的结果类型。
Attempts to convert NonZeroI128 to NonZeroUsize.
type Error = TryFromIntError
type Error = TryFromIntError
发生转换错误时返回的类型。
Attempts to convert NonZeroI16 to NonZeroUsize.
type Error = TryFromIntError
type Error = TryFromIntError
发生转换错误时返回的类型。
Attempts to convert NonZeroI32 to NonZeroUsize.
type Error = TryFromIntError
type Error = TryFromIntError
发生转换错误时返回的类型。
Attempts to convert NonZeroI64 to NonZeroUsize.
type Error = TryFromIntError
type Error = TryFromIntError
发生转换错误时返回的类型。
Attempts to convert NonZeroIsize to NonZeroUsize.
type Error = TryFromIntError
type Error = TryFromIntError
发生转换错误时返回的类型。
Attempts to convert NonZeroU128 to NonZeroUsize.
type Error = TryFromIntError
type Error = TryFromIntError
发生转换错误时返回的类型。
Attempts to convert NonZeroU32 to NonZeroUsize.
type Error = TryFromIntError
type Error = TryFromIntError
发生转换错误时返回的类型。
Attempts to convert NonZeroU64 to NonZeroUsize.
type Error = TryFromIntError
type Error = TryFromIntError
发生转换错误时返回的类型。
Attempts to convert NonZeroUsize to NonZeroU8.
type Error = TryFromIntError
type Error = TryFromIntError
发生转换错误时返回的类型。
Attempts to convert NonZeroUsize to NonZeroU16.
type Error = TryFromIntError
type Error = TryFromIntError
发生转换错误时返回的类型。
Attempts to convert NonZeroUsize to NonZeroIsize.
type Error = TryFromIntError
type Error = TryFromIntError
发生转换错误时返回的类型。
Attempts to convert NonZeroUsize to NonZeroU32.
type Error = TryFromIntError
type Error = TryFromIntError
发生转换错误时返回的类型。
Attempts to convert NonZeroUsize to NonZeroU64.
type Error = TryFromIntError
type Error = TryFromIntError
发生转换错误时返回的类型。
Attempts to convert NonZeroUsize to NonZeroU128.
type Error = TryFromIntError
type Error = TryFromIntError
发生转换错误时返回的类型。
Attempts to convert NonZeroUsize to NonZeroI8.
type Error = TryFromIntError
type Error = TryFromIntError
发生转换错误时返回的类型。
Attempts to convert NonZeroUsize to NonZeroI16.
type Error = TryFromIntError
type Error = TryFromIntError
发生转换错误时返回的类型。
Attempts to convert NonZeroUsize to NonZeroI32.
type Error = TryFromIntError
type Error = TryFromIntError
发生转换错误时返回的类型。
Attempts to convert NonZeroUsize to NonZeroI64.
type Error = TryFromIntError
type Error = TryFromIntError
发生转换错误时返回的类型。
Attempts to convert NonZeroUsize to NonZeroI128.
type Error = TryFromIntError
type Error = TryFromIntError
发生转换错误时返回的类型。