当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Rust NonZeroU16.checked_next_power_of_two用法及代码示例


本文简要介绍rust语言中 core::num::NonZeroU16.checked_next_power_of_two 的用法。

用法

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

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

例子

#![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() );

相关用法


注:本文由纯净天空筛选整理自rust-lang.org大神的英文原创作品 core::num::NonZeroU16.checked_next_power_of_two。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。