pub struct NonZero<T: ZeroablePrimitive>(/* private fields */);
generic_nonzero
#120257)Expand description
A value that is known not to equal zero.
This enables some memory layout optimization.
For example, Option<NonZero<u32>>
is the same size as u32
:
#![feature(generic_nonzero)]
use core::mem::size_of;
assert_eq!(size_of::<Option<core::num::NonZero<u32>>>(), size_of::<u32>());
RunImplementations§
source§impl<T> NonZero<T>where
T: ZeroablePrimitive,
impl<T> NonZero<T>where
T: ZeroablePrimitive,
1.28.0 (const: 1.47.0) · sourcepub const fn new(n: T) -> Option<Self>
pub const fn new(n: T) -> Option<Self>
Creates a non-zero if the given value is not zero.
1.28.0 (const: 1.28.0) · sourcepub const unsafe fn new_unchecked(n: T) -> Self
pub const unsafe fn new_unchecked(n: T) -> Self
Creates a non-zero without checking whether the value is non-zero. This results in undefined behaviour if the value is zero.
§Safety
The value must not be zero.
sourcepub fn from_mut(n: &mut T) -> Option<&mut Self>
🔬This is a nightly-only experimental API. (nonzero_from_mut
#106290)
pub fn from_mut(n: &mut T) -> Option<&mut Self>
nonzero_from_mut
#106290)Converts a reference to a non-zero mutable reference if the referenced value is not zero.
sourcepub unsafe fn from_mut_unchecked(n: &mut T) -> &mut Self
🔬This is a nightly-only experimental API. (nonzero_from_mut
#106290)
pub unsafe fn from_mut_unchecked(n: &mut T) -> &mut Self
nonzero_from_mut
#106290)Converts a mutable reference to a non-zero mutable reference without checking whether the referenced value is non-zero. This results in undefined behavior if the referenced value is zero.
§Safety
The referenced value must not be zero.
source§impl NonZero<u8>
impl NonZero<u8>
1.53.0 (const: 1.53.0) · sourcepub const fn leading_zeros(self) -> u32
pub const fn leading_zeros(self) -> u32
Returns the number of leading zeros in the binary representation of self
.
On many architectures, this function can perform better than leading_zeros()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
let n = std::num::NonZeroU8::new(u8::MAX).unwrap();
assert_eq!(n.leading_zeros(), 0);
Run1.53.0 (const: 1.53.0) · sourcepub const fn trailing_zeros(self) -> u32
pub const fn trailing_zeros(self) -> u32
Returns the number of trailing zeros in the binary representation
of self
.
On many architectures, this function can perform better than trailing_zeros()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
let n = std::num::NonZeroU8::new(0b0101000).unwrap();
assert_eq!(n.trailing_zeros(), 3);
Runconst: unstable · sourcepub fn count_ones(self) -> NonZero<u32>
🔬This is a nightly-only experimental API. (non_zero_count_ones
#120287)
pub fn count_ones(self) -> NonZero<u32>
non_zero_count_ones
#120287)Returns the number of ones in the binary representation of self
.
§Examples
Basic usage:
#![feature(generic_nonzero, non_zero_count_ones)]
let a = NonZero::<u8>::new(0b100_0000)?;
let b = NonZero::<u8>::new(0b100_0011)?;
assert_eq!(a.count_ones(), NonZero::new(1)?);
assert_eq!(b.count_ones(), NonZero::new(3)?);
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_add(self, other: u8) -> Option<Self>
pub const fn checked_add(self, other: u8) -> Option<Self>
Adds an unsigned integer to a non-zero value.
Checks for overflow and returns None
on overflow.
As a consequence, the result cannot wrap to zero.
§Examples
let one = NonZeroU8::new(1)?;
let two = NonZeroU8::new(2)?;
let max = NonZeroU8::new(u8::MAX)?;
assert_eq!(Some(two), one.checked_add(1));
assert_eq!(None, max.checked_add(1));
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_add(self, other: u8) -> Self
pub const fn saturating_add(self, other: u8) -> Self
Adds an unsigned integer to a non-zero value.
Return NonZeroU8::MAX
on overflow.
§Examples
let one = NonZeroU8::new(1)?;
let two = NonZeroU8::new(2)?;
let max = NonZeroU8::new(u8::MAX)?;
assert_eq!(two, one.saturating_add(1));
assert_eq!(max, max.saturating_add(1));
Runsourcepub const unsafe fn unchecked_add(self, other: u8) -> Self
🔬This is a nightly-only experimental API. (nonzero_ops
#84186)
pub const unsafe fn unchecked_add(self, other: u8) -> Self
nonzero_ops
#84186)Adds an unsigned integer to a non-zero value,
assuming overflow cannot occur.
Overflow is unchecked, and it is undefined behaviour to overflow
even if the result would wrap to a non-zero value.
The behaviour is undefined as soon as
self + rhs > u8::MAX
.
§Examples
#![feature(nonzero_ops)]
let one = NonZeroU8::new(1)?;
let two = NonZeroU8::new(2)?;
assert_eq!(two, unsafe { one.unchecked_add(1) });
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_next_power_of_two(self) -> Option<Self>
pub const fn checked_next_power_of_two(self) -> Option<Self>
Returns the smallest power of two greater than or equal to n.
Checks for overflow and returns None
if the next power of two is greater than the type’s maximum value.
As a consequence, the result cannot wrap to zero.
§Examples
let two = NonZeroU8::new(2)?;
let three = NonZeroU8::new(3)?;
let four = NonZeroU8::new(4)?;
let max = NonZeroU8::new(u8::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() );
Run1.67.0 (const: 1.67.0) · sourcepub const fn ilog2(self) -> u32
pub const fn ilog2(self) -> u32
Returns the base 2 logarithm of the number, rounded down.
This is the same operation as
u8::ilog2
,
except that it has no failure cases to worry about
since this value can never be zero.
§Examples
assert_eq!(NonZeroU8::new(7).unwrap().ilog2(), 2);
assert_eq!(NonZeroU8::new(8).unwrap().ilog2(), 3);
assert_eq!(NonZeroU8::new(9).unwrap().ilog2(), 3);
Run1.67.0 (const: 1.67.0) · sourcepub const fn ilog10(self) -> u32
pub const fn ilog10(self) -> u32
Returns the base 10 logarithm of the number, rounded down.
This is the same operation as
u8::ilog10
,
except that it has no failure cases to worry about
since this value can never be zero.
§Examples
assert_eq!(NonZeroU8::new(99).unwrap().ilog10(), 1);
assert_eq!(NonZeroU8::new(100).unwrap().ilog10(), 2);
assert_eq!(NonZeroU8::new(101).unwrap().ilog10(), 2);
Runconst: unstable · sourcepub fn midpoint(self, rhs: Self) -> Self
🔬This is a nightly-only experimental API. (num_midpoint
#110840)
pub fn midpoint(self, rhs: Self) -> Self
num_midpoint
#110840)Calculates the middle point of self
and rhs
.
midpoint(a, b)
is (a + b) >> 1
as if it were performed in a
sufficiently-large signed integral type. This implies that the result is
always rounded towards negative infinity and that no overflow will ever occur.
§Examples
#![feature(num_midpoint)]
let one = NonZeroU8::new(1)?;
let two = NonZeroU8::new(2)?;
let four = NonZeroU8::new(4)?;
assert_eq!(one.midpoint(four), two);
assert_eq!(four.midpoint(one), two);
Run1.59.0 (const: 1.59.0) · sourcepub const fn is_power_of_two(self) -> bool
pub const fn is_power_of_two(self) -> bool
Returns true
if and only if self == (1 << k)
for some k
.
On many architectures, this function can perform better than is_power_of_two()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
let eight = std::num::NonZeroU8::new(8).unwrap();
assert!(eight.is_power_of_two());
let ten = std::num::NonZeroU8::new(10).unwrap();
assert!(!ten.is_power_of_two());
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_mul(self, other: Self) -> Option<Self>
pub const fn checked_mul(self, other: Self) -> Option<Self>
Multiplies two non-zero integers together.
Checks for overflow and returns None
on overflow.
As a consequence, the result cannot wrap to zero.
§Examples
let two = NonZeroU8::new(2)?;
let four = NonZeroU8::new(4)?;
let max = NonZeroU8::new(u8::MAX)?;
assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_mul(self, other: Self) -> Self
pub const fn saturating_mul(self, other: Self) -> Self
Multiplies two non-zero integers together.
Return NonZeroU8::MAX
on overflow.
§Examples
let two = NonZeroU8::new(2)?;
let four = NonZeroU8::new(4)?;
let max = NonZeroU8::new(u8::MAX)?;
assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));
Runsourcepub const unsafe fn unchecked_mul(self, other: Self) -> Self
🔬This is a nightly-only experimental API. (nonzero_ops
#84186)
pub const unsafe fn unchecked_mul(self, other: Self) -> Self
nonzero_ops
#84186)Multiplies two non-zero integers together,
assuming overflow cannot occur.
Overflow is unchecked, and it is undefined behaviour to overflow
even if the result would wrap to a non-zero value.
The behaviour is undefined as soon as
self * rhs > u8::MAX
.
§Examples
#![feature(nonzero_ops)]
let two = NonZeroU8::new(2)?;
let four = NonZeroU8::new(4)?;
assert_eq!(four, unsafe { two.unchecked_mul(two) });
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_pow(self, other: u32) -> Option<Self>
pub const fn checked_pow(self, other: u32) -> Option<Self>
Raises non-zero value to an integer power.
Checks for overflow and returns None
on overflow.
As a consequence, the result cannot wrap to zero.
§Examples
let three = NonZeroU8::new(3)?;
let twenty_seven = NonZeroU8::new(27)?;
let half_max = NonZeroU8::new(u8::MAX / 2)?;
assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_pow(self, other: u32) -> Self
pub const fn saturating_pow(self, other: u32) -> Self
Raise non-zero value to an integer power.
Return NonZeroU8::MAX
on overflow.
§Examples
let three = NonZeroU8::new(3)?;
let twenty_seven = NonZeroU8::new(27)?;
let max = NonZeroU8::new(u8::MAX)?;
assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));
Runsource§impl NonZero<u16>
impl NonZero<u16>
1.53.0 (const: 1.53.0) · sourcepub const fn leading_zeros(self) -> u32
pub const fn leading_zeros(self) -> u32
Returns the number of leading zeros in the binary representation of self
.
On many architectures, this function can perform better than leading_zeros()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
let n = std::num::NonZeroU16::new(u16::MAX).unwrap();
assert_eq!(n.leading_zeros(), 0);
Run1.53.0 (const: 1.53.0) · sourcepub const fn trailing_zeros(self) -> u32
pub const fn trailing_zeros(self) -> u32
Returns the number of trailing zeros in the binary representation
of self
.
On many architectures, this function can perform better than trailing_zeros()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
let n = std::num::NonZeroU16::new(0b0101000).unwrap();
assert_eq!(n.trailing_zeros(), 3);
Runconst: unstable · sourcepub fn count_ones(self) -> NonZero<u32>
🔬This is a nightly-only experimental API. (non_zero_count_ones
#120287)
pub fn count_ones(self) -> NonZero<u32>
non_zero_count_ones
#120287)Returns the number of ones in the binary representation of self
.
§Examples
Basic usage:
#![feature(generic_nonzero, non_zero_count_ones)]
let a = NonZero::<u16>::new(0b100_0000)?;
let b = NonZero::<u16>::new(0b100_0011)?;
assert_eq!(a.count_ones(), NonZero::new(1)?);
assert_eq!(b.count_ones(), NonZero::new(3)?);
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_add(self, other: u16) -> Option<Self>
pub const fn checked_add(self, other: u16) -> Option<Self>
Adds an unsigned integer to a non-zero value.
Checks for overflow and returns None
on overflow.
As a consequence, the result cannot wrap to zero.
§Examples
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));
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_add(self, other: u16) -> Self
pub const fn saturating_add(self, other: u16) -> Self
Adds an unsigned integer to a non-zero value.
Return NonZeroU16::MAX
on overflow.
§Examples
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));
Runsourcepub const unsafe fn unchecked_add(self, other: u16) -> Self
🔬This is a nightly-only experimental API. (nonzero_ops
#84186)
pub const unsafe fn unchecked_add(self, other: u16) -> Self
nonzero_ops
#84186)Adds an unsigned integer to a non-zero value,
assuming overflow cannot occur.
Overflow is unchecked, and it is undefined behaviour to overflow
even if the result would wrap to a non-zero value.
The behaviour is undefined as soon as
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) });
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_next_power_of_two(self) -> Option<Self>
pub const fn checked_next_power_of_two(self) -> Option<Self>
Returns the smallest power of two greater than or equal to n.
Checks for overflow and returns None
if the next power of two is greater than the type’s maximum value.
As a consequence, the result cannot wrap to zero.
§Examples
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() );
Run1.67.0 (const: 1.67.0) · sourcepub const fn ilog2(self) -> u32
pub const fn ilog2(self) -> u32
Returns the base 2 logarithm of the number, rounded down.
This is the same operation as
u16::ilog2
,
except that it has no failure cases to worry about
since this value can never be zero.
§Examples
assert_eq!(NonZeroU16::new(7).unwrap().ilog2(), 2);
assert_eq!(NonZeroU16::new(8).unwrap().ilog2(), 3);
assert_eq!(NonZeroU16::new(9).unwrap().ilog2(), 3);
Run1.67.0 (const: 1.67.0) · sourcepub const fn ilog10(self) -> u32
pub const fn ilog10(self) -> u32
Returns the base 10 logarithm of the number, rounded down.
This is the same operation as
u16::ilog10
,
except that it has no failure cases to worry about
since this value can never be zero.
§Examples
assert_eq!(NonZeroU16::new(99).unwrap().ilog10(), 1);
assert_eq!(NonZeroU16::new(100).unwrap().ilog10(), 2);
assert_eq!(NonZeroU16::new(101).unwrap().ilog10(), 2);
Runconst: unstable · sourcepub fn midpoint(self, rhs: Self) -> Self
🔬This is a nightly-only experimental API. (num_midpoint
#110840)
pub fn midpoint(self, rhs: Self) -> Self
num_midpoint
#110840)Calculates the middle point of self
and rhs
.
midpoint(a, b)
is (a + b) >> 1
as if it were performed in a
sufficiently-large signed integral type. This implies that the result is
always rounded towards negative infinity and that no overflow will ever occur.
§Examples
#![feature(num_midpoint)]
let one = NonZeroU16::new(1)?;
let two = NonZeroU16::new(2)?;
let four = NonZeroU16::new(4)?;
assert_eq!(one.midpoint(four), two);
assert_eq!(four.midpoint(one), two);
Run1.59.0 (const: 1.59.0) · sourcepub const fn is_power_of_two(self) -> bool
pub const fn is_power_of_two(self) -> bool
Returns true
if and only if self == (1 << k)
for some k
.
On many architectures, this function can perform better than is_power_of_two()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
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());
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_mul(self, other: Self) -> Option<Self>
pub const fn checked_mul(self, other: Self) -> Option<Self>
Multiplies two non-zero integers together.
Checks for overflow and returns None
on overflow.
As a consequence, the result cannot wrap to zero.
§Examples
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));
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_mul(self, other: Self) -> Self
pub const fn saturating_mul(self, other: Self) -> Self
Multiplies two non-zero integers together.
Return NonZeroU16::MAX
on overflow.
§Examples
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));
Runsourcepub const unsafe fn unchecked_mul(self, other: Self) -> Self
🔬This is a nightly-only experimental API. (nonzero_ops
#84186)
pub const unsafe fn unchecked_mul(self, other: Self) -> Self
nonzero_ops
#84186)Multiplies two non-zero integers together,
assuming overflow cannot occur.
Overflow is unchecked, and it is undefined behaviour to overflow
even if the result would wrap to a non-zero value.
The behaviour is undefined as soon as
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) });
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_pow(self, other: u32) -> Option<Self>
pub const fn checked_pow(self, other: u32) -> Option<Self>
Raises non-zero value to an integer power.
Checks for overflow and returns None
on overflow.
As a consequence, the result cannot wrap to zero.
§Examples
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));
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_pow(self, other: u32) -> Self
pub const fn saturating_pow(self, other: u32) -> Self
Raise non-zero value to an integer power.
Return NonZeroU16::MAX
on overflow.
§Examples
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));
Runsource§impl NonZero<u32>
impl NonZero<u32>
1.53.0 (const: 1.53.0) · sourcepub const fn leading_zeros(self) -> u32
pub const fn leading_zeros(self) -> u32
Returns the number of leading zeros in the binary representation of self
.
On many architectures, this function can perform better than leading_zeros()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
let n = std::num::NonZeroU32::new(u32::MAX).unwrap();
assert_eq!(n.leading_zeros(), 0);
Run1.53.0 (const: 1.53.0) · sourcepub const fn trailing_zeros(self) -> u32
pub const fn trailing_zeros(self) -> u32
Returns the number of trailing zeros in the binary representation
of self
.
On many architectures, this function can perform better than trailing_zeros()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
let n = std::num::NonZeroU32::new(0b0101000).unwrap();
assert_eq!(n.trailing_zeros(), 3);
Runconst: unstable · sourcepub fn count_ones(self) -> NonZero<u32>
🔬This is a nightly-only experimental API. (non_zero_count_ones
#120287)
pub fn count_ones(self) -> NonZero<u32>
non_zero_count_ones
#120287)Returns the number of ones in the binary representation of self
.
§Examples
Basic usage:
#![feature(generic_nonzero, non_zero_count_ones)]
let a = NonZero::<u32>::new(0b100_0000)?;
let b = NonZero::<u32>::new(0b100_0011)?;
assert_eq!(a.count_ones(), NonZero::new(1)?);
assert_eq!(b.count_ones(), NonZero::new(3)?);
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_add(self, other: u32) -> Option<Self>
pub const fn checked_add(self, other: u32) -> Option<Self>
Adds an unsigned integer to a non-zero value.
Checks for overflow and returns None
on overflow.
As a consequence, the result cannot wrap to zero.
§Examples
let one = NonZeroU32::new(1)?;
let two = NonZeroU32::new(2)?;
let max = NonZeroU32::new(u32::MAX)?;
assert_eq!(Some(two), one.checked_add(1));
assert_eq!(None, max.checked_add(1));
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_add(self, other: u32) -> Self
pub const fn saturating_add(self, other: u32) -> Self
Adds an unsigned integer to a non-zero value.
Return NonZeroU32::MAX
on overflow.
§Examples
let one = NonZeroU32::new(1)?;
let two = NonZeroU32::new(2)?;
let max = NonZeroU32::new(u32::MAX)?;
assert_eq!(two, one.saturating_add(1));
assert_eq!(max, max.saturating_add(1));
Runsourcepub const unsafe fn unchecked_add(self, other: u32) -> Self
🔬This is a nightly-only experimental API. (nonzero_ops
#84186)
pub const unsafe fn unchecked_add(self, other: u32) -> Self
nonzero_ops
#84186)Adds an unsigned integer to a non-zero value,
assuming overflow cannot occur.
Overflow is unchecked, and it is undefined behaviour to overflow
even if the result would wrap to a non-zero value.
The behaviour is undefined as soon as
self + rhs > u32::MAX
.
§Examples
#![feature(nonzero_ops)]
let one = NonZeroU32::new(1)?;
let two = NonZeroU32::new(2)?;
assert_eq!(two, unsafe { one.unchecked_add(1) });
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_next_power_of_two(self) -> Option<Self>
pub const fn checked_next_power_of_two(self) -> Option<Self>
Returns the smallest power of two greater than or equal to n.
Checks for overflow and returns None
if the next power of two is greater than the type’s maximum value.
As a consequence, the result cannot wrap to zero.
§Examples
let two = NonZeroU32::new(2)?;
let three = NonZeroU32::new(3)?;
let four = NonZeroU32::new(4)?;
let max = NonZeroU32::new(u32::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() );
Run1.67.0 (const: 1.67.0) · sourcepub const fn ilog2(self) -> u32
pub const fn ilog2(self) -> u32
Returns the base 2 logarithm of the number, rounded down.
This is the same operation as
u32::ilog2
,
except that it has no failure cases to worry about
since this value can never be zero.
§Examples
assert_eq!(NonZeroU32::new(7).unwrap().ilog2(), 2);
assert_eq!(NonZeroU32::new(8).unwrap().ilog2(), 3);
assert_eq!(NonZeroU32::new(9).unwrap().ilog2(), 3);
Run1.67.0 (const: 1.67.0) · sourcepub const fn ilog10(self) -> u32
pub const fn ilog10(self) -> u32
Returns the base 10 logarithm of the number, rounded down.
This is the same operation as
u32::ilog10
,
except that it has no failure cases to worry about
since this value can never be zero.
§Examples
assert_eq!(NonZeroU32::new(99).unwrap().ilog10(), 1);
assert_eq!(NonZeroU32::new(100).unwrap().ilog10(), 2);
assert_eq!(NonZeroU32::new(101).unwrap().ilog10(), 2);
Runconst: unstable · sourcepub fn midpoint(self, rhs: Self) -> Self
🔬This is a nightly-only experimental API. (num_midpoint
#110840)
pub fn midpoint(self, rhs: Self) -> Self
num_midpoint
#110840)Calculates the middle point of self
and rhs
.
midpoint(a, b)
is (a + b) >> 1
as if it were performed in a
sufficiently-large signed integral type. This implies that the result is
always rounded towards negative infinity and that no overflow will ever occur.
§Examples
#![feature(num_midpoint)]
let one = NonZeroU32::new(1)?;
let two = NonZeroU32::new(2)?;
let four = NonZeroU32::new(4)?;
assert_eq!(one.midpoint(four), two);
assert_eq!(four.midpoint(one), two);
Run1.59.0 (const: 1.59.0) · sourcepub const fn is_power_of_two(self) -> bool
pub const fn is_power_of_two(self) -> bool
Returns true
if and only if self == (1 << k)
for some k
.
On many architectures, this function can perform better than is_power_of_two()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
let eight = std::num::NonZeroU32::new(8).unwrap();
assert!(eight.is_power_of_two());
let ten = std::num::NonZeroU32::new(10).unwrap();
assert!(!ten.is_power_of_two());
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_mul(self, other: Self) -> Option<Self>
pub const fn checked_mul(self, other: Self) -> Option<Self>
Multiplies two non-zero integers together.
Checks for overflow and returns None
on overflow.
As a consequence, the result cannot wrap to zero.
§Examples
let two = NonZeroU32::new(2)?;
let four = NonZeroU32::new(4)?;
let max = NonZeroU32::new(u32::MAX)?;
assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_mul(self, other: Self) -> Self
pub const fn saturating_mul(self, other: Self) -> Self
Multiplies two non-zero integers together.
Return NonZeroU32::MAX
on overflow.
§Examples
let two = NonZeroU32::new(2)?;
let four = NonZeroU32::new(4)?;
let max = NonZeroU32::new(u32::MAX)?;
assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));
Runsourcepub const unsafe fn unchecked_mul(self, other: Self) -> Self
🔬This is a nightly-only experimental API. (nonzero_ops
#84186)
pub const unsafe fn unchecked_mul(self, other: Self) -> Self
nonzero_ops
#84186)Multiplies two non-zero integers together,
assuming overflow cannot occur.
Overflow is unchecked, and it is undefined behaviour to overflow
even if the result would wrap to a non-zero value.
The behaviour is undefined as soon as
self * rhs > u32::MAX
.
§Examples
#![feature(nonzero_ops)]
let two = NonZeroU32::new(2)?;
let four = NonZeroU32::new(4)?;
assert_eq!(four, unsafe { two.unchecked_mul(two) });
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_pow(self, other: u32) -> Option<Self>
pub const fn checked_pow(self, other: u32) -> Option<Self>
Raises non-zero value to an integer power.
Checks for overflow and returns None
on overflow.
As a consequence, the result cannot wrap to zero.
§Examples
let three = NonZeroU32::new(3)?;
let twenty_seven = NonZeroU32::new(27)?;
let half_max = NonZeroU32::new(u32::MAX / 2)?;
assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_pow(self, other: u32) -> Self
pub const fn saturating_pow(self, other: u32) -> Self
Raise non-zero value to an integer power.
Return NonZeroU32::MAX
on overflow.
§Examples
let three = NonZeroU32::new(3)?;
let twenty_seven = NonZeroU32::new(27)?;
let max = NonZeroU32::new(u32::MAX)?;
assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));
Runsource§impl NonZero<u64>
impl NonZero<u64>
1.53.0 (const: 1.53.0) · sourcepub const fn leading_zeros(self) -> u32
pub const fn leading_zeros(self) -> u32
Returns the number of leading zeros in the binary representation of self
.
On many architectures, this function can perform better than leading_zeros()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
let n = std::num::NonZeroU64::new(u64::MAX).unwrap();
assert_eq!(n.leading_zeros(), 0);
Run1.53.0 (const: 1.53.0) · sourcepub const fn trailing_zeros(self) -> u32
pub const fn trailing_zeros(self) -> u32
Returns the number of trailing zeros in the binary representation
of self
.
On many architectures, this function can perform better than trailing_zeros()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
let n = std::num::NonZeroU64::new(0b0101000).unwrap();
assert_eq!(n.trailing_zeros(), 3);
Runconst: unstable · sourcepub fn count_ones(self) -> NonZero<u32>
🔬This is a nightly-only experimental API. (non_zero_count_ones
#120287)
pub fn count_ones(self) -> NonZero<u32>
non_zero_count_ones
#120287)Returns the number of ones in the binary representation of self
.
§Examples
Basic usage:
#![feature(generic_nonzero, non_zero_count_ones)]
let a = NonZero::<u64>::new(0b100_0000)?;
let b = NonZero::<u64>::new(0b100_0011)?;
assert_eq!(a.count_ones(), NonZero::new(1)?);
assert_eq!(b.count_ones(), NonZero::new(3)?);
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_add(self, other: u64) -> Option<Self>
pub const fn checked_add(self, other: u64) -> Option<Self>
Adds an unsigned integer to a non-zero value.
Checks for overflow and returns None
on overflow.
As a consequence, the result cannot wrap to zero.
§Examples
let one = NonZeroU64::new(1)?;
let two = NonZeroU64::new(2)?;
let max = NonZeroU64::new(u64::MAX)?;
assert_eq!(Some(two), one.checked_add(1));
assert_eq!(None, max.checked_add(1));
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_add(self, other: u64) -> Self
pub const fn saturating_add(self, other: u64) -> Self
Adds an unsigned integer to a non-zero value.
Return NonZeroU64::MAX
on overflow.
§Examples
let one = NonZeroU64::new(1)?;
let two = NonZeroU64::new(2)?;
let max = NonZeroU64::new(u64::MAX)?;
assert_eq!(two, one.saturating_add(1));
assert_eq!(max, max.saturating_add(1));
Runsourcepub const unsafe fn unchecked_add(self, other: u64) -> Self
🔬This is a nightly-only experimental API. (nonzero_ops
#84186)
pub const unsafe fn unchecked_add(self, other: u64) -> Self
nonzero_ops
#84186)Adds an unsigned integer to a non-zero value,
assuming overflow cannot occur.
Overflow is unchecked, and it is undefined behaviour to overflow
even if the result would wrap to a non-zero value.
The behaviour is undefined as soon as
self + rhs > u64::MAX
.
§Examples
#![feature(nonzero_ops)]
let one = NonZeroU64::new(1)?;
let two = NonZeroU64::new(2)?;
assert_eq!(two, unsafe { one.unchecked_add(1) });
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_next_power_of_two(self) -> Option<Self>
pub const fn checked_next_power_of_two(self) -> Option<Self>
Returns the smallest power of two greater than or equal to n.
Checks for overflow and returns None
if the next power of two is greater than the type’s maximum value.
As a consequence, the result cannot wrap to zero.
§Examples
let two = NonZeroU64::new(2)?;
let three = NonZeroU64::new(3)?;
let four = NonZeroU64::new(4)?;
let max = NonZeroU64::new(u64::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() );
Run1.67.0 (const: 1.67.0) · sourcepub const fn ilog2(self) -> u32
pub const fn ilog2(self) -> u32
Returns the base 2 logarithm of the number, rounded down.
This is the same operation as
u64::ilog2
,
except that it has no failure cases to worry about
since this value can never be zero.
§Examples
assert_eq!(NonZeroU64::new(7).unwrap().ilog2(), 2);
assert_eq!(NonZeroU64::new(8).unwrap().ilog2(), 3);
assert_eq!(NonZeroU64::new(9).unwrap().ilog2(), 3);
Run1.67.0 (const: 1.67.0) · sourcepub const fn ilog10(self) -> u32
pub const fn ilog10(self) -> u32
Returns the base 10 logarithm of the number, rounded down.
This is the same operation as
u64::ilog10
,
except that it has no failure cases to worry about
since this value can never be zero.
§Examples
assert_eq!(NonZeroU64::new(99).unwrap().ilog10(), 1);
assert_eq!(NonZeroU64::new(100).unwrap().ilog10(), 2);
assert_eq!(NonZeroU64::new(101).unwrap().ilog10(), 2);
Runconst: unstable · sourcepub fn midpoint(self, rhs: Self) -> Self
🔬This is a nightly-only experimental API. (num_midpoint
#110840)
pub fn midpoint(self, rhs: Self) -> Self
num_midpoint
#110840)Calculates the middle point of self
and rhs
.
midpoint(a, b)
is (a + b) >> 1
as if it were performed in a
sufficiently-large signed integral type. This implies that the result is
always rounded towards negative infinity and that no overflow will ever occur.
§Examples
#![feature(num_midpoint)]
let one = NonZeroU64::new(1)?;
let two = NonZeroU64::new(2)?;
let four = NonZeroU64::new(4)?;
assert_eq!(one.midpoint(four), two);
assert_eq!(four.midpoint(one), two);
Run1.59.0 (const: 1.59.0) · sourcepub const fn is_power_of_two(self) -> bool
pub const fn is_power_of_two(self) -> bool
Returns true
if and only if self == (1 << k)
for some k
.
On many architectures, this function can perform better than is_power_of_two()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
let eight = std::num::NonZeroU64::new(8).unwrap();
assert!(eight.is_power_of_two());
let ten = std::num::NonZeroU64::new(10).unwrap();
assert!(!ten.is_power_of_two());
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_mul(self, other: Self) -> Option<Self>
pub const fn checked_mul(self, other: Self) -> Option<Self>
Multiplies two non-zero integers together.
Checks for overflow and returns None
on overflow.
As a consequence, the result cannot wrap to zero.
§Examples
let two = NonZeroU64::new(2)?;
let four = NonZeroU64::new(4)?;
let max = NonZeroU64::new(u64::MAX)?;
assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_mul(self, other: Self) -> Self
pub const fn saturating_mul(self, other: Self) -> Self
Multiplies two non-zero integers together.
Return NonZeroU64::MAX
on overflow.
§Examples
let two = NonZeroU64::new(2)?;
let four = NonZeroU64::new(4)?;
let max = NonZeroU64::new(u64::MAX)?;
assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));
Runsourcepub const unsafe fn unchecked_mul(self, other: Self) -> Self
🔬This is a nightly-only experimental API. (nonzero_ops
#84186)
pub const unsafe fn unchecked_mul(self, other: Self) -> Self
nonzero_ops
#84186)Multiplies two non-zero integers together,
assuming overflow cannot occur.
Overflow is unchecked, and it is undefined behaviour to overflow
even if the result would wrap to a non-zero value.
The behaviour is undefined as soon as
self * rhs > u64::MAX
.
§Examples
#![feature(nonzero_ops)]
let two = NonZeroU64::new(2)?;
let four = NonZeroU64::new(4)?;
assert_eq!(four, unsafe { two.unchecked_mul(two) });
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_pow(self, other: u32) -> Option<Self>
pub const fn checked_pow(self, other: u32) -> Option<Self>
Raises non-zero value to an integer power.
Checks for overflow and returns None
on overflow.
As a consequence, the result cannot wrap to zero.
§Examples
let three = NonZeroU64::new(3)?;
let twenty_seven = NonZeroU64::new(27)?;
let half_max = NonZeroU64::new(u64::MAX / 2)?;
assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_pow(self, other: u32) -> Self
pub const fn saturating_pow(self, other: u32) -> Self
Raise non-zero value to an integer power.
Return NonZeroU64::MAX
on overflow.
§Examples
let three = NonZeroU64::new(3)?;
let twenty_seven = NonZeroU64::new(27)?;
let max = NonZeroU64::new(u64::MAX)?;
assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));
Runsource§impl NonZero<u128>
impl NonZero<u128>
1.67.0 · sourcepub const BITS: u32 = 128u32
pub const BITS: u32 = 128u32
The size of this non-zero integer type in bits.
This value is equal to u128::BITS
.
§Examples
assert_eq!(NonZeroU128::BITS, u128::BITS);
Run1.53.0 (const: 1.53.0) · sourcepub const fn leading_zeros(self) -> u32
pub const fn leading_zeros(self) -> u32
Returns the number of leading zeros in the binary representation of self
.
On many architectures, this function can perform better than leading_zeros()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
let n = std::num::NonZeroU128::new(u128::MAX).unwrap();
assert_eq!(n.leading_zeros(), 0);
Run1.53.0 (const: 1.53.0) · sourcepub const fn trailing_zeros(self) -> u32
pub const fn trailing_zeros(self) -> u32
Returns the number of trailing zeros in the binary representation
of self
.
On many architectures, this function can perform better than trailing_zeros()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
let n = std::num::NonZeroU128::new(0b0101000).unwrap();
assert_eq!(n.trailing_zeros(), 3);
Runconst: unstable · sourcepub fn count_ones(self) -> NonZero<u32>
🔬This is a nightly-only experimental API. (non_zero_count_ones
#120287)
pub fn count_ones(self) -> NonZero<u32>
non_zero_count_ones
#120287)Returns the number of ones in the binary representation of self
.
§Examples
Basic usage:
#![feature(generic_nonzero, non_zero_count_ones)]
let a = NonZero::<u128>::new(0b100_0000)?;
let b = NonZero::<u128>::new(0b100_0011)?;
assert_eq!(a.count_ones(), NonZero::new(1)?);
assert_eq!(b.count_ones(), NonZero::new(3)?);
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_add(self, other: u128) -> Option<Self>
pub const fn checked_add(self, other: u128) -> Option<Self>
Adds an unsigned integer to a non-zero value.
Checks for overflow and returns None
on overflow.
As a consequence, the result cannot wrap to zero.
§Examples
let one = NonZeroU128::new(1)?;
let two = NonZeroU128::new(2)?;
let max = NonZeroU128::new(u128::MAX)?;
assert_eq!(Some(two), one.checked_add(1));
assert_eq!(None, max.checked_add(1));
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_add(self, other: u128) -> Self
pub const fn saturating_add(self, other: u128) -> Self
Adds an unsigned integer to a non-zero value.
Return NonZeroU128::MAX
on overflow.
§Examples
let one = NonZeroU128::new(1)?;
let two = NonZeroU128::new(2)?;
let max = NonZeroU128::new(u128::MAX)?;
assert_eq!(two, one.saturating_add(1));
assert_eq!(max, max.saturating_add(1));
Runsourcepub const unsafe fn unchecked_add(self, other: u128) -> Self
🔬This is a nightly-only experimental API. (nonzero_ops
#84186)
pub const unsafe fn unchecked_add(self, other: u128) -> Self
nonzero_ops
#84186)Adds an unsigned integer to a non-zero value,
assuming overflow cannot occur.
Overflow is unchecked, and it is undefined behaviour to overflow
even if the result would wrap to a non-zero value.
The behaviour is undefined as soon as
self + rhs > u128::MAX
.
§Examples
#![feature(nonzero_ops)]
let one = NonZeroU128::new(1)?;
let two = NonZeroU128::new(2)?;
assert_eq!(two, unsafe { one.unchecked_add(1) });
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_next_power_of_two(self) -> Option<Self>
pub const fn checked_next_power_of_two(self) -> Option<Self>
Returns the smallest power of two greater than or equal to n.
Checks for overflow and returns None
if the next power of two is greater than the type’s maximum value.
As a consequence, the result cannot wrap to zero.
§Examples
let two = NonZeroU128::new(2)?;
let three = NonZeroU128::new(3)?;
let four = NonZeroU128::new(4)?;
let max = NonZeroU128::new(u128::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() );
Run1.67.0 (const: 1.67.0) · sourcepub const fn ilog2(self) -> u32
pub const fn ilog2(self) -> u32
Returns the base 2 logarithm of the number, rounded down.
This is the same operation as
u128::ilog2
,
except that it has no failure cases to worry about
since this value can never be zero.
§Examples
assert_eq!(NonZeroU128::new(7).unwrap().ilog2(), 2);
assert_eq!(NonZeroU128::new(8).unwrap().ilog2(), 3);
assert_eq!(NonZeroU128::new(9).unwrap().ilog2(), 3);
Run1.67.0 (const: 1.67.0) · sourcepub const fn ilog10(self) -> u32
pub const fn ilog10(self) -> u32
Returns the base 10 logarithm of the number, rounded down.
This is the same operation as
u128::ilog10
,
except that it has no failure cases to worry about
since this value can never be zero.
§Examples
assert_eq!(NonZeroU128::new(99).unwrap().ilog10(), 1);
assert_eq!(NonZeroU128::new(100).unwrap().ilog10(), 2);
assert_eq!(NonZeroU128::new(101).unwrap().ilog10(), 2);
Runconst: unstable · sourcepub fn midpoint(self, rhs: Self) -> Self
🔬This is a nightly-only experimental API. (num_midpoint
#110840)
pub fn midpoint(self, rhs: Self) -> Self
num_midpoint
#110840)Calculates the middle point of self
and rhs
.
midpoint(a, b)
is (a + b) >> 1
as if it were performed in a
sufficiently-large signed integral type. This implies that the result is
always rounded towards negative infinity and that no overflow will ever occur.
§Examples
#![feature(num_midpoint)]
let one = NonZeroU128::new(1)?;
let two = NonZeroU128::new(2)?;
let four = NonZeroU128::new(4)?;
assert_eq!(one.midpoint(four), two);
assert_eq!(four.midpoint(one), two);
Run1.59.0 (const: 1.59.0) · sourcepub const fn is_power_of_two(self) -> bool
pub const fn is_power_of_two(self) -> bool
Returns true
if and only if self == (1 << k)
for some k
.
On many architectures, this function can perform better than is_power_of_two()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
let eight = std::num::NonZeroU128::new(8).unwrap();
assert!(eight.is_power_of_two());
let ten = std::num::NonZeroU128::new(10).unwrap();
assert!(!ten.is_power_of_two());
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_mul(self, other: Self) -> Option<Self>
pub const fn checked_mul(self, other: Self) -> Option<Self>
Multiplies two non-zero integers together.
Checks for overflow and returns None
on overflow.
As a consequence, the result cannot wrap to zero.
§Examples
let two = NonZeroU128::new(2)?;
let four = NonZeroU128::new(4)?;
let max = NonZeroU128::new(u128::MAX)?;
assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_mul(self, other: Self) -> Self
pub const fn saturating_mul(self, other: Self) -> Self
Multiplies two non-zero integers together.
Return NonZeroU128::MAX
on overflow.
§Examples
let two = NonZeroU128::new(2)?;
let four = NonZeroU128::new(4)?;
let max = NonZeroU128::new(u128::MAX)?;
assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));
Runsourcepub const unsafe fn unchecked_mul(self, other: Self) -> Self
🔬This is a nightly-only experimental API. (nonzero_ops
#84186)
pub const unsafe fn unchecked_mul(self, other: Self) -> Self
nonzero_ops
#84186)Multiplies two non-zero integers together,
assuming overflow cannot occur.
Overflow is unchecked, and it is undefined behaviour to overflow
even if the result would wrap to a non-zero value.
The behaviour is undefined as soon as
self * rhs > u128::MAX
.
§Examples
#![feature(nonzero_ops)]
let two = NonZeroU128::new(2)?;
let four = NonZeroU128::new(4)?;
assert_eq!(four, unsafe { two.unchecked_mul(two) });
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_pow(self, other: u32) -> Option<Self>
pub const fn checked_pow(self, other: u32) -> Option<Self>
Raises non-zero value to an integer power.
Checks for overflow and returns None
on overflow.
As a consequence, the result cannot wrap to zero.
§Examples
let three = NonZeroU128::new(3)?;
let twenty_seven = NonZeroU128::new(27)?;
let half_max = NonZeroU128::new(u128::MAX / 2)?;
assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_pow(self, other: u32) -> Self
pub const fn saturating_pow(self, other: u32) -> Self
Raise non-zero value to an integer power.
Return NonZeroU128::MAX
on overflow.
§Examples
let three = NonZeroU128::new(3)?;
let twenty_seven = NonZeroU128::new(27)?;
let max = NonZeroU128::new(u128::MAX)?;
assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));
Runsource§impl NonZero<usize>
impl NonZero<usize>
1.67.0 · sourcepub const BITS: u32 = 64u32
pub const BITS: u32 = 64u32
The size of this non-zero integer type in bits.
This value is equal to usize::BITS
.
§Examples
assert_eq!(NonZeroUsize::BITS, usize::BITS);
Run1.53.0 (const: 1.53.0) · sourcepub const fn leading_zeros(self) -> u32
pub const fn leading_zeros(self) -> u32
Returns the number of leading zeros in the binary representation of self
.
On many architectures, this function can perform better than leading_zeros()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
let n = std::num::NonZeroUsize::new(usize::MAX).unwrap();
assert_eq!(n.leading_zeros(), 0);
Run1.53.0 (const: 1.53.0) · sourcepub const fn trailing_zeros(self) -> u32
pub const fn trailing_zeros(self) -> u32
Returns the number of trailing zeros in the binary representation
of self
.
On many architectures, this function can perform better than trailing_zeros()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
let n = std::num::NonZeroUsize::new(0b0101000).unwrap();
assert_eq!(n.trailing_zeros(), 3);
Runconst: unstable · sourcepub fn count_ones(self) -> NonZero<u32>
🔬This is a nightly-only experimental API. (non_zero_count_ones
#120287)
pub fn count_ones(self) -> NonZero<u32>
non_zero_count_ones
#120287)Returns the number of ones in the binary representation of self
.
§Examples
Basic usage:
#![feature(generic_nonzero, non_zero_count_ones)]
let a = NonZero::<usize>::new(0b100_0000)?;
let b = NonZero::<usize>::new(0b100_0011)?;
assert_eq!(a.count_ones(), NonZero::new(1)?);
assert_eq!(b.count_ones(), NonZero::new(3)?);
Run1.70.0 · sourcepub const MAX: Self = _
pub const MAX: Self = _
The largest value that can be represented by this non-zero
integer type,
equal to usize::MAX
.
§Examples
assert_eq!(NonZeroUsize::MAX.get(), usize::MAX);
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_add(self, other: usize) -> Option<Self>
pub const fn checked_add(self, other: usize) -> Option<Self>
Adds an unsigned integer to a non-zero value.
Checks for overflow and returns None
on overflow.
As a consequence, the result cannot wrap to zero.
§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));
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_add(self, other: usize) -> Self
pub const fn saturating_add(self, other: usize) -> Self
Adds an unsigned integer to a non-zero value.
Return NonZeroUsize::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));
Runsourcepub const unsafe fn unchecked_add(self, other: usize) -> Self
🔬This is a nightly-only experimental API. (nonzero_ops
#84186)
pub const unsafe fn unchecked_add(self, other: usize) -> Self
nonzero_ops
#84186)Adds an unsigned integer to a non-zero value,
assuming overflow cannot occur.
Overflow is unchecked, and it is undefined behaviour to overflow
even if the result would wrap to a non-zero value.
The behaviour is undefined as soon as
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) });
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_next_power_of_two(self) -> Option<Self>
pub const fn checked_next_power_of_two(self) -> Option<Self>
Returns the smallest power of two greater than or equal to n.
Checks for overflow and returns None
if the next power of two is greater than the type’s maximum value.
As a consequence, the result cannot wrap to zero.
§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() );
Run1.67.0 (const: 1.67.0) · sourcepub const fn ilog2(self) -> u32
pub const fn ilog2(self) -> u32
Returns the base 2 logarithm of the number, rounded down.
This is the same operation as
usize::ilog2
,
except that it has no failure cases to worry about
since this value can never be zero.
§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);
Run1.67.0 (const: 1.67.0) · sourcepub const fn ilog10(self) -> u32
pub const fn ilog10(self) -> u32
Returns the base 10 logarithm of the number, rounded down.
This is the same operation as
usize::ilog10
,
except that it has no failure cases to worry about
since this value can never be zero.
§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);
Runconst: unstable · sourcepub fn midpoint(self, rhs: Self) -> Self
🔬This is a nightly-only experimental API. (num_midpoint
#110840)
pub fn midpoint(self, rhs: Self) -> Self
num_midpoint
#110840)Calculates the middle point of self
and rhs
.
midpoint(a, b)
is (a + b) >> 1
as if it were performed in a
sufficiently-large signed integral type. This implies that the result is
always rounded towards negative infinity and that no overflow will ever occur.
§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);
Run1.59.0 (const: 1.59.0) · sourcepub const fn is_power_of_two(self) -> bool
pub const fn is_power_of_two(self) -> bool
Returns true
if and only if self == (1 << k)
for some k
.
On many architectures, this function can perform better than is_power_of_two()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
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());
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_mul(self, other: Self) -> Option<Self>
pub const fn checked_mul(self, other: Self) -> Option<Self>
Multiplies two non-zero integers together.
Checks for overflow and returns None
on overflow.
As a consequence, the result cannot wrap to zero.
§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));
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_mul(self, other: Self) -> Self
pub const fn saturating_mul(self, other: Self) -> Self
Multiplies two non-zero integers together.
Return NonZeroUsize::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));
Runsourcepub const unsafe fn unchecked_mul(self, other: Self) -> Self
🔬This is a nightly-only experimental API. (nonzero_ops
#84186)
pub const unsafe fn unchecked_mul(self, other: Self) -> Self
nonzero_ops
#84186)Multiplies two non-zero integers together,
assuming overflow cannot occur.
Overflow is unchecked, and it is undefined behaviour to overflow
even if the result would wrap to a non-zero value.
The behaviour is undefined as soon as
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) });
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_pow(self, other: u32) -> Option<Self>
pub const fn checked_pow(self, other: u32) -> Option<Self>
Raises non-zero value to an integer power.
Checks for overflow and returns None
on overflow.
As a consequence, the result cannot wrap to zero.
§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));
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_pow(self, other: u32) -> Self
pub const fn saturating_pow(self, other: u32) -> Self
Raise non-zero value to an integer power.
Return NonZeroUsize::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));
Runsource§impl NonZero<i8>
impl NonZero<i8>
1.53.0 (const: 1.53.0) · sourcepub const fn leading_zeros(self) -> u32
pub const fn leading_zeros(self) -> u32
Returns the number of leading zeros in the binary representation of self
.
On many architectures, this function can perform better than leading_zeros()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
let n = std::num::NonZeroI8::new(-1i8).unwrap();
assert_eq!(n.leading_zeros(), 0);
Run1.53.0 (const: 1.53.0) · sourcepub const fn trailing_zeros(self) -> u32
pub const fn trailing_zeros(self) -> u32
Returns the number of trailing zeros in the binary representation
of self
.
On many architectures, this function can perform better than trailing_zeros()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
let n = std::num::NonZeroI8::new(0b0101000).unwrap();
assert_eq!(n.trailing_zeros(), 3);
Runconst: unstable · sourcepub fn count_ones(self) -> NonZero<u32>
🔬This is a nightly-only experimental API. (non_zero_count_ones
#120287)
pub fn count_ones(self) -> NonZero<u32>
non_zero_count_ones
#120287)Returns the number of ones in the binary representation of self
.
§Examples
Basic usage:
#![feature(generic_nonzero, non_zero_count_ones)]
let a = NonZero::<i8>::new(0b100_0000)?;
let b = NonZero::<i8>::new(0b100_0011)?;
assert_eq!(a.count_ones(), NonZero::new(1)?);
assert_eq!(b.count_ones(), NonZero::new(3)?);
Run1.70.0 · sourcepub const MIN: Self = _
pub const MIN: Self = _
The smallest value that can be represented by this non-zero
integer type,
equal to i8::MIN
.
Note: While most integer types are defined for every whole
number between MIN
and MAX
, signed non-zero integers are
a special case. They have a “gap” at 0.
§Examples
assert_eq!(NonZeroI8::MIN.get(), i8::MIN);
Run1.70.0 · sourcepub const MAX: Self = _
pub const MAX: Self = _
The largest value that can be represented by this non-zero
integer type,
equal to i8::MAX
.
Note: While most integer types are defined for every whole
number between MIN
and MAX
, signed non-zero integers are
a special case. They have a “gap” at 0.
§Examples
assert_eq!(NonZeroI8::MAX.get(), i8::MAX);
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_abs(self) -> Option<Self>
pub const fn checked_abs(self) -> Option<Self>
Checked absolute value.
Checks for overflow and returns None
if
self == NonZeroI8::MIN
.
The result cannot be zero.
§Example
let pos = NonZeroI8::new(1)?;
let neg = NonZeroI8::new(-1)?;
let min = NonZeroI8::new(i8::MIN)?;
assert_eq!(Some(pos), neg.checked_abs());
assert_eq!(None, min.checked_abs());
Run1.64.0 (const: 1.64.0) · sourcepub const fn overflowing_abs(self) -> (Self, bool)
pub const fn overflowing_abs(self) -> (Self, bool)
Computes the absolute value of self,
with overflow information, see
i8::overflowing_abs
.
§Example
let pos = NonZeroI8::new(1)?;
let neg = NonZeroI8::new(-1)?;
let min = NonZeroI8::new(i8::MIN)?;
assert_eq!((pos, false), pos.overflowing_abs());
assert_eq!((pos, false), neg.overflowing_abs());
assert_eq!((min, true), min.overflowing_abs());
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_abs(self) -> Self
pub const fn saturating_abs(self) -> Self
Saturating absolute value, see
i8::saturating_abs
.
§Example
let pos = NonZeroI8::new(1)?;
let neg = NonZeroI8::new(-1)?;
let min = NonZeroI8::new(i8::MIN)?;
let min_plus = NonZeroI8::new(i8::MIN + 1)?;
let max = NonZeroI8::new(i8::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());
Run1.64.0 (const: 1.64.0) · sourcepub const fn wrapping_abs(self) -> Self
pub const fn wrapping_abs(self) -> Self
Wrapping absolute value, see
i8::wrapping_abs
.
§Example
let pos = NonZeroI8::new(1)?;
let neg = NonZeroI8::new(-1)?;
let min = NonZeroI8::new(i8::MIN)?;
assert_eq!(pos, pos.wrapping_abs());
assert_eq!(pos, neg.wrapping_abs());
assert_eq!(min, min.wrapping_abs());
assert_eq!(max, (-max).wrapping_abs());
Run1.64.0 (const: 1.64.0) · sourcepub const fn unsigned_abs(self) -> NonZeroU8
pub const fn unsigned_abs(self) -> NonZeroU8
Computes the absolute value of self without any wrapping or panicking.
§Example
let u_pos = NonZeroU8::new(1)?;
let i_pos = NonZeroI8::new(1)?;
let i_neg = NonZeroI8::new(-1)?;
let i_min = NonZeroI8::new(i8::MIN)?;
let u_max = NonZeroU8::new(u8::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());
Run1.71.0 (const: 1.71.0) · sourcepub const fn is_positive(self) -> bool
pub const fn is_positive(self) -> bool
1.71.0 (const: 1.71.0) · sourcepub const fn is_negative(self) -> bool
pub const fn is_negative(self) -> bool
1.71.0 (const: 1.71.0) · sourcepub const fn checked_neg(self) -> Option<Self>
pub const fn checked_neg(self) -> Option<Self>
1.71.0 (const: 1.71.0) · sourcepub const fn overflowing_neg(self) -> (Self, bool)
pub const fn overflowing_neg(self) -> (Self, bool)
Negates self, overflowing if this is equal to the minimum value.
See i8::overflowing_neg
for documentation on overflow behaviour.
§Example
let pos_five = NonZeroI8::new(5)?;
let neg_five = NonZeroI8::new(-5)?;
let min = NonZeroI8::new(i8::MIN)?;
assert_eq!(pos_five.overflowing_neg(), (neg_five, false));
assert_eq!(min.overflowing_neg(), (min, true));
Run1.71.0 (const: 1.71.0) · sourcepub const fn saturating_neg(self) -> Self
pub const fn saturating_neg(self) -> Self
Saturating negation. Computes -self
,
returning NonZeroI8::MAX
if self == NonZeroI8::MIN
instead of overflowing.
§Example
let pos_five = NonZeroI8::new(5)?;
let neg_five = NonZeroI8::new(-5)?;
let min = NonZeroI8::new(i8::MIN)?;
let min_plus_one = NonZeroI8::new(i8::MIN + 1)?;
let max = NonZeroI8::new(i8::MAX)?;
assert_eq!(pos_five.saturating_neg(), neg_five);
assert_eq!(min.saturating_neg(), max);
assert_eq!(max.saturating_neg(), min_plus_one);
Run1.71.0 (const: 1.71.0) · sourcepub const fn wrapping_neg(self) -> Self
pub const fn wrapping_neg(self) -> Self
Wrapping (modular) negation. Computes -self
, wrapping around at the boundary
of the type.
See i8::wrapping_neg
for documentation on overflow behaviour.
§Example
let pos_five = NonZeroI8::new(5)?;
let neg_five = NonZeroI8::new(-5)?;
let min = NonZeroI8::new(i8::MIN)?;
assert_eq!(pos_five.wrapping_neg(), neg_five);
assert_eq!(min.wrapping_neg(), min);
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_mul(self, other: Self) -> Option<Self>
pub const fn checked_mul(self, other: Self) -> Option<Self>
Multiplies two non-zero integers together.
Checks for overflow and returns None
on overflow.
As a consequence, the result cannot wrap to zero.
§Examples
let two = NonZeroI8::new(2)?;
let four = NonZeroI8::new(4)?;
let max = NonZeroI8::new(i8::MAX)?;
assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_mul(self, other: Self) -> Self
pub const fn saturating_mul(self, other: Self) -> Self
Multiplies two non-zero integers together.
Return NonZeroI8::MAX
on overflow.
§Examples
let two = NonZeroI8::new(2)?;
let four = NonZeroI8::new(4)?;
let max = NonZeroI8::new(i8::MAX)?;
assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));
Runsourcepub const unsafe fn unchecked_mul(self, other: Self) -> Self
🔬This is a nightly-only experimental API. (nonzero_ops
#84186)
pub const unsafe fn unchecked_mul(self, other: Self) -> Self
nonzero_ops
#84186)Multiplies two non-zero integers together,
assuming overflow cannot occur.
Overflow is unchecked, and it is undefined behaviour to overflow
even if the result would wrap to a non-zero value.
The behaviour is undefined as soon as
self * rhs > i8::MAX
, or self * rhs < i8::MIN
.
§Examples
#![feature(nonzero_ops)]
let two = NonZeroI8::new(2)?;
let four = NonZeroI8::new(4)?;
assert_eq!(four, unsafe { two.unchecked_mul(two) });
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_pow(self, other: u32) -> Option<Self>
pub const fn checked_pow(self, other: u32) -> Option<Self>
Raises non-zero value to an integer power.
Checks for overflow and returns None
on overflow.
As a consequence, the result cannot wrap to zero.
§Examples
let three = NonZeroI8::new(3)?;
let twenty_seven = NonZeroI8::new(27)?;
let half_max = NonZeroI8::new(i8::MAX / 2)?;
assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_pow(self, other: u32) -> Self
pub const fn saturating_pow(self, other: u32) -> Self
Raise non-zero value to an integer power.
Return NonZeroI8::MIN
or NonZeroI8::MAX
on overflow.
§Examples
let three = NonZeroI8::new(3)?;
let twenty_seven = NonZeroI8::new(27)?;
let max = NonZeroI8::new(i8::MAX)?;
assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));
Runsource§impl NonZero<i16>
impl NonZero<i16>
1.53.0 (const: 1.53.0) · sourcepub const fn leading_zeros(self) -> u32
pub const fn leading_zeros(self) -> u32
Returns the number of leading zeros in the binary representation of self
.
On many architectures, this function can perform better than leading_zeros()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
let n = std::num::NonZeroI16::new(-1i16).unwrap();
assert_eq!(n.leading_zeros(), 0);
Run1.53.0 (const: 1.53.0) · sourcepub const fn trailing_zeros(self) -> u32
pub const fn trailing_zeros(self) -> u32
Returns the number of trailing zeros in the binary representation
of self
.
On many architectures, this function can perform better than trailing_zeros()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
let n = std::num::NonZeroI16::new(0b0101000).unwrap();
assert_eq!(n.trailing_zeros(), 3);
Runconst: unstable · sourcepub fn count_ones(self) -> NonZero<u32>
🔬This is a nightly-only experimental API. (non_zero_count_ones
#120287)
pub fn count_ones(self) -> NonZero<u32>
non_zero_count_ones
#120287)Returns the number of ones in the binary representation of self
.
§Examples
Basic usage:
#![feature(generic_nonzero, non_zero_count_ones)]
let a = NonZero::<i16>::new(0b100_0000)?;
let b = NonZero::<i16>::new(0b100_0011)?;
assert_eq!(a.count_ones(), NonZero::new(1)?);
assert_eq!(b.count_ones(), NonZero::new(3)?);
Run1.70.0 · sourcepub const MIN: Self = _
pub const MIN: Self = _
The smallest value that can be represented by this non-zero
integer type,
equal to i16::MIN
.
Note: While most integer types are defined for every whole
number between MIN
and MAX
, signed non-zero integers are
a special case. They have a “gap” at 0.
§Examples
assert_eq!(NonZeroI16::MIN.get(), i16::MIN);
Run1.70.0 · sourcepub const MAX: Self = _
pub const MAX: Self = _
The largest value that can be represented by this non-zero
integer type,
equal to i16::MAX
.
Note: While most integer types are defined for every whole
number between MIN
and MAX
, signed non-zero integers are
a special case. They have a “gap” at 0.
§Examples
assert_eq!(NonZeroI16::MAX.get(), i16::MAX);
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_abs(self) -> Option<Self>
pub const fn checked_abs(self) -> Option<Self>
Checked absolute value.
Checks for overflow and returns None
if
self == NonZeroI16::MIN
.
The result cannot be zero.
§Example
let pos = NonZeroI16::new(1)?;
let neg = NonZeroI16::new(-1)?;
let min = NonZeroI16::new(i16::MIN)?;
assert_eq!(Some(pos), neg.checked_abs());
assert_eq!(None, min.checked_abs());
Run1.64.0 (const: 1.64.0) · sourcepub const fn overflowing_abs(self) -> (Self, bool)
pub const fn overflowing_abs(self) -> (Self, bool)
Computes the absolute value of self,
with overflow information, see
i16::overflowing_abs
.
§Example
let pos = NonZeroI16::new(1)?;
let neg = NonZeroI16::new(-1)?;
let min = NonZeroI16::new(i16::MIN)?;
assert_eq!((pos, false), pos.overflowing_abs());
assert_eq!((pos, false), neg.overflowing_abs());
assert_eq!((min, true), min.overflowing_abs());
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_abs(self) -> Self
pub const fn saturating_abs(self) -> Self
Saturating absolute value, see
i16::saturating_abs
.
§Example
let pos = NonZeroI16::new(1)?;
let neg = NonZeroI16::new(-1)?;
let min = NonZeroI16::new(i16::MIN)?;
let min_plus = NonZeroI16::new(i16::MIN + 1)?;
let max = NonZeroI16::new(i16::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());
Run1.64.0 (const: 1.64.0) · sourcepub const fn wrapping_abs(self) -> Self
pub const fn wrapping_abs(self) -> Self
Wrapping absolute value, see
i16::wrapping_abs
.
§Example
let pos = NonZeroI16::new(1)?;
let neg = NonZeroI16::new(-1)?;
let min = NonZeroI16::new(i16::MIN)?;
assert_eq!(pos, pos.wrapping_abs());
assert_eq!(pos, neg.wrapping_abs());
assert_eq!(min, min.wrapping_abs());
assert_eq!(max, (-max).wrapping_abs());
Run1.64.0 (const: 1.64.0) · sourcepub const fn unsigned_abs(self) -> NonZeroU16
pub const fn unsigned_abs(self) -> NonZeroU16
Computes the absolute value of self without any wrapping or panicking.
§Example
let u_pos = NonZeroU16::new(1)?;
let i_pos = NonZeroI16::new(1)?;
let i_neg = NonZeroI16::new(-1)?;
let i_min = NonZeroI16::new(i16::MIN)?;
let u_max = NonZeroU16::new(u16::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());
Run1.71.0 (const: 1.71.0) · sourcepub const fn is_positive(self) -> bool
pub const fn is_positive(self) -> bool
1.71.0 (const: 1.71.0) · sourcepub const fn is_negative(self) -> bool
pub const fn is_negative(self) -> bool
1.71.0 (const: 1.71.0) · sourcepub const fn checked_neg(self) -> Option<Self>
pub const fn checked_neg(self) -> Option<Self>
1.71.0 (const: 1.71.0) · sourcepub const fn overflowing_neg(self) -> (Self, bool)
pub const fn overflowing_neg(self) -> (Self, bool)
Negates self, overflowing if this is equal to the minimum value.
See i16::overflowing_neg
for documentation on overflow behaviour.
§Example
let pos_five = NonZeroI16::new(5)?;
let neg_five = NonZeroI16::new(-5)?;
let min = NonZeroI16::new(i16::MIN)?;
assert_eq!(pos_five.overflowing_neg(), (neg_five, false));
assert_eq!(min.overflowing_neg(), (min, true));
Run1.71.0 (const: 1.71.0) · sourcepub const fn saturating_neg(self) -> Self
pub const fn saturating_neg(self) -> Self
Saturating negation. Computes -self
,
returning NonZeroI16::MAX
if self == NonZeroI16::MIN
instead of overflowing.
§Example
let pos_five = NonZeroI16::new(5)?;
let neg_five = NonZeroI16::new(-5)?;
let min = NonZeroI16::new(i16::MIN)?;
let min_plus_one = NonZeroI16::new(i16::MIN + 1)?;
let max = NonZeroI16::new(i16::MAX)?;
assert_eq!(pos_five.saturating_neg(), neg_five);
assert_eq!(min.saturating_neg(), max);
assert_eq!(max.saturating_neg(), min_plus_one);
Run1.71.0 (const: 1.71.0) · sourcepub const fn wrapping_neg(self) -> Self
pub const fn wrapping_neg(self) -> Self
Wrapping (modular) negation. Computes -self
, wrapping around at the boundary
of the type.
See i16::wrapping_neg
for documentation on overflow behaviour.
§Example
let pos_five = NonZeroI16::new(5)?;
let neg_five = NonZeroI16::new(-5)?;
let min = NonZeroI16::new(i16::MIN)?;
assert_eq!(pos_five.wrapping_neg(), neg_five);
assert_eq!(min.wrapping_neg(), min);
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_mul(self, other: Self) -> Option<Self>
pub const fn checked_mul(self, other: Self) -> Option<Self>
Multiplies two non-zero integers together.
Checks for overflow and returns None
on overflow.
As a consequence, the result cannot wrap to zero.
§Examples
let two = NonZeroI16::new(2)?;
let four = NonZeroI16::new(4)?;
let max = NonZeroI16::new(i16::MAX)?;
assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_mul(self, other: Self) -> Self
pub const fn saturating_mul(self, other: Self) -> Self
Multiplies two non-zero integers together.
Return NonZeroI16::MAX
on overflow.
§Examples
let two = NonZeroI16::new(2)?;
let four = NonZeroI16::new(4)?;
let max = NonZeroI16::new(i16::MAX)?;
assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));
Runsourcepub const unsafe fn unchecked_mul(self, other: Self) -> Self
🔬This is a nightly-only experimental API. (nonzero_ops
#84186)
pub const unsafe fn unchecked_mul(self, other: Self) -> Self
nonzero_ops
#84186)Multiplies two non-zero integers together,
assuming overflow cannot occur.
Overflow is unchecked, and it is undefined behaviour to overflow
even if the result would wrap to a non-zero value.
The behaviour is undefined as soon as
self * rhs > i16::MAX
, or self * rhs < i16::MIN
.
§Examples
#![feature(nonzero_ops)]
let two = NonZeroI16::new(2)?;
let four = NonZeroI16::new(4)?;
assert_eq!(four, unsafe { two.unchecked_mul(two) });
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_pow(self, other: u32) -> Option<Self>
pub const fn checked_pow(self, other: u32) -> Option<Self>
Raises non-zero value to an integer power.
Checks for overflow and returns None
on overflow.
As a consequence, the result cannot wrap to zero.
§Examples
let three = NonZeroI16::new(3)?;
let twenty_seven = NonZeroI16::new(27)?;
let half_max = NonZeroI16::new(i16::MAX / 2)?;
assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_pow(self, other: u32) -> Self
pub const fn saturating_pow(self, other: u32) -> Self
Raise non-zero value to an integer power.
Return NonZeroI16::MIN
or NonZeroI16::MAX
on overflow.
§Examples
let three = NonZeroI16::new(3)?;
let twenty_seven = NonZeroI16::new(27)?;
let max = NonZeroI16::new(i16::MAX)?;
assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));
Runsource§impl NonZero<i32>
impl NonZero<i32>
1.53.0 (const: 1.53.0) · sourcepub const fn leading_zeros(self) -> u32
pub const fn leading_zeros(self) -> u32
Returns the number of leading zeros in the binary representation of self
.
On many architectures, this function can perform better than leading_zeros()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
let n = std::num::NonZeroI32::new(-1i32).unwrap();
assert_eq!(n.leading_zeros(), 0);
Run1.53.0 (const: 1.53.0) · sourcepub const fn trailing_zeros(self) -> u32
pub const fn trailing_zeros(self) -> u32
Returns the number of trailing zeros in the binary representation
of self
.
On many architectures, this function can perform better than trailing_zeros()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
let n = std::num::NonZeroI32::new(0b0101000).unwrap();
assert_eq!(n.trailing_zeros(), 3);
Runconst: unstable · sourcepub fn count_ones(self) -> NonZero<u32>
🔬This is a nightly-only experimental API. (non_zero_count_ones
#120287)
pub fn count_ones(self) -> NonZero<u32>
non_zero_count_ones
#120287)Returns the number of ones in the binary representation of self
.
§Examples
Basic usage:
#![feature(generic_nonzero, non_zero_count_ones)]
let a = NonZero::<i32>::new(0b100_0000)?;
let b = NonZero::<i32>::new(0b100_0011)?;
assert_eq!(a.count_ones(), NonZero::new(1)?);
assert_eq!(b.count_ones(), NonZero::new(3)?);
Run1.70.0 · sourcepub const MIN: Self = _
pub const MIN: Self = _
The smallest value that can be represented by this non-zero
integer type,
equal to i32::MIN
.
Note: While most integer types are defined for every whole
number between MIN
and MAX
, signed non-zero integers are
a special case. They have a “gap” at 0.
§Examples
assert_eq!(NonZeroI32::MIN.get(), i32::MIN);
Run1.70.0 · sourcepub const MAX: Self = _
pub const MAX: Self = _
The largest value that can be represented by this non-zero
integer type,
equal to i32::MAX
.
Note: While most integer types are defined for every whole
number between MIN
and MAX
, signed non-zero integers are
a special case. They have a “gap” at 0.
§Examples
assert_eq!(NonZeroI32::MAX.get(), i32::MAX);
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_abs(self) -> Option<Self>
pub const fn checked_abs(self) -> Option<Self>
Checked absolute value.
Checks for overflow and returns None
if
self == NonZeroI32::MIN
.
The result cannot be zero.
§Example
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());
Run1.64.0 (const: 1.64.0) · sourcepub const fn overflowing_abs(self) -> (Self, bool)
pub const fn overflowing_abs(self) -> (Self, bool)
Computes the absolute value of self,
with overflow information, see
i32::overflowing_abs
.
§Example
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());
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_abs(self) -> Self
pub const fn saturating_abs(self) -> Self
Saturating absolute value, see
i32::saturating_abs
.
§Example
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());
Run1.64.0 (const: 1.64.0) · sourcepub const fn wrapping_abs(self) -> Self
pub const fn wrapping_abs(self) -> Self
Wrapping absolute value, see
i32::wrapping_abs
.
§Example
let pos = NonZeroI32::new(1)?;
let neg = NonZeroI32::new(-1)?;
let min = NonZeroI32::new(i32::MIN)?;
assert_eq!(pos, pos.wrapping_abs());
assert_eq!(pos, neg.wrapping_abs());
assert_eq!(min, min.wrapping_abs());
assert_eq!(max, (-max).wrapping_abs());
Run1.64.0 (const: 1.64.0) · sourcepub const fn unsigned_abs(self) -> NonZeroU32
pub const fn unsigned_abs(self) -> NonZeroU32
Computes the absolute value of self without any wrapping or panicking.
§Example
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());
Run1.71.0 (const: 1.71.0) · sourcepub const fn is_positive(self) -> bool
pub const fn is_positive(self) -> bool
1.71.0 (const: 1.71.0) · sourcepub const fn is_negative(self) -> bool
pub const fn is_negative(self) -> bool
1.71.0 (const: 1.71.0) · sourcepub const fn checked_neg(self) -> Option<Self>
pub const fn checked_neg(self) -> Option<Self>
1.71.0 (const: 1.71.0) · sourcepub const fn overflowing_neg(self) -> (Self, bool)
pub const fn overflowing_neg(self) -> (Self, bool)
Negates self, overflowing if this is equal to the minimum value.
See i32::overflowing_neg
for documentation on overflow behaviour.
§Example
let pos_five = NonZeroI32::new(5)?;
let neg_five = NonZeroI32::new(-5)?;
let min = NonZeroI32::new(i32::MIN)?;
assert_eq!(pos_five.overflowing_neg(), (neg_five, false));
assert_eq!(min.overflowing_neg(), (min, true));
Run1.71.0 (const: 1.71.0) · sourcepub const fn saturating_neg(self) -> Self
pub const fn saturating_neg(self) -> Self
Saturating negation. Computes -self
,
returning NonZeroI32::MAX
if self == NonZeroI32::MIN
instead of overflowing.
§Example
let pos_five = NonZeroI32::new(5)?;
let neg_five = NonZeroI32::new(-5)?;
let min = NonZeroI32::new(i32::MIN)?;
let min_plus_one = NonZeroI32::new(i32::MIN + 1)?;
let max = NonZeroI32::new(i32::MAX)?;
assert_eq!(pos_five.saturating_neg(), neg_five);
assert_eq!(min.saturating_neg(), max);
assert_eq!(max.saturating_neg(), min_plus_one);
Run1.71.0 (const: 1.71.0) · sourcepub const fn wrapping_neg(self) -> Self
pub const fn wrapping_neg(self) -> Self
Wrapping (modular) negation. Computes -self
, wrapping around at the boundary
of the type.
See i32::wrapping_neg
for documentation on overflow behaviour.
§Example
let pos_five = NonZeroI32::new(5)?;
let neg_five = NonZeroI32::new(-5)?;
let min = NonZeroI32::new(i32::MIN)?;
assert_eq!(pos_five.wrapping_neg(), neg_five);
assert_eq!(min.wrapping_neg(), min);
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_mul(self, other: Self) -> Option<Self>
pub const fn checked_mul(self, other: Self) -> Option<Self>
Multiplies two non-zero integers together.
Checks for overflow and returns None
on overflow.
As a consequence, the result cannot wrap to zero.
§Examples
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));
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_mul(self, other: Self) -> Self
pub const fn saturating_mul(self, other: Self) -> Self
Multiplies two non-zero integers together.
Return NonZeroI32::MAX
on overflow.
§Examples
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));
Runsourcepub const unsafe fn unchecked_mul(self, other: Self) -> Self
🔬This is a nightly-only experimental API. (nonzero_ops
#84186)
pub const unsafe fn unchecked_mul(self, other: Self) -> Self
nonzero_ops
#84186)Multiplies two non-zero integers together,
assuming overflow cannot occur.
Overflow is unchecked, and it is undefined behaviour to overflow
even if the result would wrap to a non-zero value.
The behaviour is undefined as soon as
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) });
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_pow(self, other: u32) -> Option<Self>
pub const fn checked_pow(self, other: u32) -> Option<Self>
Raises non-zero value to an integer power.
Checks for overflow and returns None
on overflow.
As a consequence, the result cannot wrap to zero.
§Examples
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));
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_pow(self, other: u32) -> Self
pub const fn saturating_pow(self, other: u32) -> Self
Raise non-zero value to an integer power.
Return NonZeroI32::MIN
or NonZeroI32::MAX
on overflow.
§Examples
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));
Runsource§impl NonZero<i64>
impl NonZero<i64>
1.53.0 (const: 1.53.0) · sourcepub const fn leading_zeros(self) -> u32
pub const fn leading_zeros(self) -> u32
Returns the number of leading zeros in the binary representation of self
.
On many architectures, this function can perform better than leading_zeros()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
let n = std::num::NonZeroI64::new(-1i64).unwrap();
assert_eq!(n.leading_zeros(), 0);
Run1.53.0 (const: 1.53.0) · sourcepub const fn trailing_zeros(self) -> u32
pub const fn trailing_zeros(self) -> u32
Returns the number of trailing zeros in the binary representation
of self
.
On many architectures, this function can perform better than trailing_zeros()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
let n = std::num::NonZeroI64::new(0b0101000).unwrap();
assert_eq!(n.trailing_zeros(), 3);
Runconst: unstable · sourcepub fn count_ones(self) -> NonZero<u32>
🔬This is a nightly-only experimental API. (non_zero_count_ones
#120287)
pub fn count_ones(self) -> NonZero<u32>
non_zero_count_ones
#120287)Returns the number of ones in the binary representation of self
.
§Examples
Basic usage:
#![feature(generic_nonzero, non_zero_count_ones)]
let a = NonZero::<i64>::new(0b100_0000)?;
let b = NonZero::<i64>::new(0b100_0011)?;
assert_eq!(a.count_ones(), NonZero::new(1)?);
assert_eq!(b.count_ones(), NonZero::new(3)?);
Run1.70.0 · sourcepub const MIN: Self = _
pub const MIN: Self = _
The smallest value that can be represented by this non-zero
integer type,
equal to i64::MIN
.
Note: While most integer types are defined for every whole
number between MIN
and MAX
, signed non-zero integers are
a special case. They have a “gap” at 0.
§Examples
assert_eq!(NonZeroI64::MIN.get(), i64::MIN);
Run1.70.0 · sourcepub const MAX: Self = _
pub const MAX: Self = _
The largest value that can be represented by this non-zero
integer type,
equal to i64::MAX
.
Note: While most integer types are defined for every whole
number between MIN
and MAX
, signed non-zero integers are
a special case. They have a “gap” at 0.
§Examples
assert_eq!(NonZeroI64::MAX.get(), i64::MAX);
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_abs(self) -> Option<Self>
pub const fn checked_abs(self) -> Option<Self>
Checked absolute value.
Checks for overflow and returns None
if
self == NonZeroI64::MIN
.
The result cannot be zero.
§Example
let pos = NonZeroI64::new(1)?;
let neg = NonZeroI64::new(-1)?;
let min = NonZeroI64::new(i64::MIN)?;
assert_eq!(Some(pos), neg.checked_abs());
assert_eq!(None, min.checked_abs());
Run1.64.0 (const: 1.64.0) · sourcepub const fn overflowing_abs(self) -> (Self, bool)
pub const fn overflowing_abs(self) -> (Self, bool)
Computes the absolute value of self,
with overflow information, see
i64::overflowing_abs
.
§Example
let pos = NonZeroI64::new(1)?;
let neg = NonZeroI64::new(-1)?;
let min = NonZeroI64::new(i64::MIN)?;
assert_eq!((pos, false), pos.overflowing_abs());
assert_eq!((pos, false), neg.overflowing_abs());
assert_eq!((min, true), min.overflowing_abs());
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_abs(self) -> Self
pub const fn saturating_abs(self) -> Self
Saturating absolute value, see
i64::saturating_abs
.
§Example
let pos = NonZeroI64::new(1)?;
let neg = NonZeroI64::new(-1)?;
let min = NonZeroI64::new(i64::MIN)?;
let min_plus = NonZeroI64::new(i64::MIN + 1)?;
let max = NonZeroI64::new(i64::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());
Run1.64.0 (const: 1.64.0) · sourcepub const fn wrapping_abs(self) -> Self
pub const fn wrapping_abs(self) -> Self
Wrapping absolute value, see
i64::wrapping_abs
.
§Example
let pos = NonZeroI64::new(1)?;
let neg = NonZeroI64::new(-1)?;
let min = NonZeroI64::new(i64::MIN)?;
assert_eq!(pos, pos.wrapping_abs());
assert_eq!(pos, neg.wrapping_abs());
assert_eq!(min, min.wrapping_abs());
assert_eq!(max, (-max).wrapping_abs());
Run1.64.0 (const: 1.64.0) · sourcepub const fn unsigned_abs(self) -> NonZeroU64
pub const fn unsigned_abs(self) -> NonZeroU64
Computes the absolute value of self without any wrapping or panicking.
§Example
let u_pos = NonZeroU64::new(1)?;
let i_pos = NonZeroI64::new(1)?;
let i_neg = NonZeroI64::new(-1)?;
let i_min = NonZeroI64::new(i64::MIN)?;
let u_max = NonZeroU64::new(u64::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());
Run1.71.0 (const: 1.71.0) · sourcepub const fn is_positive(self) -> bool
pub const fn is_positive(self) -> bool
1.71.0 (const: 1.71.0) · sourcepub const fn is_negative(self) -> bool
pub const fn is_negative(self) -> bool
1.71.0 (const: 1.71.0) · sourcepub const fn checked_neg(self) -> Option<Self>
pub const fn checked_neg(self) -> Option<Self>
1.71.0 (const: 1.71.0) · sourcepub const fn overflowing_neg(self) -> (Self, bool)
pub const fn overflowing_neg(self) -> (Self, bool)
Negates self, overflowing if this is equal to the minimum value.
See i64::overflowing_neg
for documentation on overflow behaviour.
§Example
let pos_five = NonZeroI64::new(5)?;
let neg_five = NonZeroI64::new(-5)?;
let min = NonZeroI64::new(i64::MIN)?;
assert_eq!(pos_five.overflowing_neg(), (neg_five, false));
assert_eq!(min.overflowing_neg(), (min, true));
Run1.71.0 (const: 1.71.0) · sourcepub const fn saturating_neg(self) -> Self
pub const fn saturating_neg(self) -> Self
Saturating negation. Computes -self
,
returning NonZeroI64::MAX
if self == NonZeroI64::MIN
instead of overflowing.
§Example
let pos_five = NonZeroI64::new(5)?;
let neg_five = NonZeroI64::new(-5)?;
let min = NonZeroI64::new(i64::MIN)?;
let min_plus_one = NonZeroI64::new(i64::MIN + 1)?;
let max = NonZeroI64::new(i64::MAX)?;
assert_eq!(pos_five.saturating_neg(), neg_five);
assert_eq!(min.saturating_neg(), max);
assert_eq!(max.saturating_neg(), min_plus_one);
Run1.71.0 (const: 1.71.0) · sourcepub const fn wrapping_neg(self) -> Self
pub const fn wrapping_neg(self) -> Self
Wrapping (modular) negation. Computes -self
, wrapping around at the boundary
of the type.
See i64::wrapping_neg
for documentation on overflow behaviour.
§Example
let pos_five = NonZeroI64::new(5)?;
let neg_five = NonZeroI64::new(-5)?;
let min = NonZeroI64::new(i64::MIN)?;
assert_eq!(pos_five.wrapping_neg(), neg_five);
assert_eq!(min.wrapping_neg(), min);
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_mul(self, other: Self) -> Option<Self>
pub const fn checked_mul(self, other: Self) -> Option<Self>
Multiplies two non-zero integers together.
Checks for overflow and returns None
on overflow.
As a consequence, the result cannot wrap to zero.
§Examples
let two = NonZeroI64::new(2)?;
let four = NonZeroI64::new(4)?;
let max = NonZeroI64::new(i64::MAX)?;
assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_mul(self, other: Self) -> Self
pub const fn saturating_mul(self, other: Self) -> Self
Multiplies two non-zero integers together.
Return NonZeroI64::MAX
on overflow.
§Examples
let two = NonZeroI64::new(2)?;
let four = NonZeroI64::new(4)?;
let max = NonZeroI64::new(i64::MAX)?;
assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));
Runsourcepub const unsafe fn unchecked_mul(self, other: Self) -> Self
🔬This is a nightly-only experimental API. (nonzero_ops
#84186)
pub const unsafe fn unchecked_mul(self, other: Self) -> Self
nonzero_ops
#84186)Multiplies two non-zero integers together,
assuming overflow cannot occur.
Overflow is unchecked, and it is undefined behaviour to overflow
even if the result would wrap to a non-zero value.
The behaviour is undefined as soon as
self * rhs > i64::MAX
, or self * rhs < i64::MIN
.
§Examples
#![feature(nonzero_ops)]
let two = NonZeroI64::new(2)?;
let four = NonZeroI64::new(4)?;
assert_eq!(four, unsafe { two.unchecked_mul(two) });
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_pow(self, other: u32) -> Option<Self>
pub const fn checked_pow(self, other: u32) -> Option<Self>
Raises non-zero value to an integer power.
Checks for overflow and returns None
on overflow.
As a consequence, the result cannot wrap to zero.
§Examples
let three = NonZeroI64::new(3)?;
let twenty_seven = NonZeroI64::new(27)?;
let half_max = NonZeroI64::new(i64::MAX / 2)?;
assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_pow(self, other: u32) -> Self
pub const fn saturating_pow(self, other: u32) -> Self
Raise non-zero value to an integer power.
Return NonZeroI64::MIN
or NonZeroI64::MAX
on overflow.
§Examples
let three = NonZeroI64::new(3)?;
let twenty_seven = NonZeroI64::new(27)?;
let max = NonZeroI64::new(i64::MAX)?;
assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));
Runsource§impl NonZero<i128>
impl NonZero<i128>
1.67.0 · sourcepub const BITS: u32 = 128u32
pub const BITS: u32 = 128u32
The size of this non-zero integer type in bits.
This value is equal to i128::BITS
.
§Examples
assert_eq!(NonZeroI128::BITS, i128::BITS);
Run1.53.0 (const: 1.53.0) · sourcepub const fn leading_zeros(self) -> u32
pub const fn leading_zeros(self) -> u32
Returns the number of leading zeros in the binary representation of self
.
On many architectures, this function can perform better than leading_zeros()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
let n = std::num::NonZeroI128::new(-1i128).unwrap();
assert_eq!(n.leading_zeros(), 0);
Run1.53.0 (const: 1.53.0) · sourcepub const fn trailing_zeros(self) -> u32
pub const fn trailing_zeros(self) -> u32
Returns the number of trailing zeros in the binary representation
of self
.
On many architectures, this function can perform better than trailing_zeros()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
let n = std::num::NonZeroI128::new(0b0101000).unwrap();
assert_eq!(n.trailing_zeros(), 3);
Runconst: unstable · sourcepub fn count_ones(self) -> NonZero<u32>
🔬This is a nightly-only experimental API. (non_zero_count_ones
#120287)
pub fn count_ones(self) -> NonZero<u32>
non_zero_count_ones
#120287)Returns the number of ones in the binary representation of self
.
§Examples
Basic usage:
#![feature(generic_nonzero, non_zero_count_ones)]
let a = NonZero::<i128>::new(0b100_0000)?;
let b = NonZero::<i128>::new(0b100_0011)?;
assert_eq!(a.count_ones(), NonZero::new(1)?);
assert_eq!(b.count_ones(), NonZero::new(3)?);
Run1.70.0 · sourcepub const MIN: Self = _
pub const MIN: Self = _
The smallest value that can be represented by this non-zero
integer type,
equal to i128::MIN
.
Note: While most integer types are defined for every whole
number between MIN
and MAX
, signed non-zero integers are
a special case. They have a “gap” at 0.
§Examples
assert_eq!(NonZeroI128::MIN.get(), i128::MIN);
Run1.70.0 · sourcepub const MAX: Self = _
pub const MAX: Self = _
The largest value that can be represented by this non-zero
integer type,
equal to i128::MAX
.
Note: While most integer types are defined for every whole
number between MIN
and MAX
, signed non-zero integers are
a special case. They have a “gap” at 0.
§Examples
assert_eq!(NonZeroI128::MAX.get(), i128::MAX);
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_abs(self) -> Option<Self>
pub const fn checked_abs(self) -> Option<Self>
Checked absolute value.
Checks for overflow and returns None
if
self == NonZeroI128::MIN
.
The result cannot be zero.
§Example
let pos = NonZeroI128::new(1)?;
let neg = NonZeroI128::new(-1)?;
let min = NonZeroI128::new(i128::MIN)?;
assert_eq!(Some(pos), neg.checked_abs());
assert_eq!(None, min.checked_abs());
Run1.64.0 (const: 1.64.0) · sourcepub const fn overflowing_abs(self) -> (Self, bool)
pub const fn overflowing_abs(self) -> (Self, bool)
Computes the absolute value of self,
with overflow information, see
i128::overflowing_abs
.
§Example
let pos = NonZeroI128::new(1)?;
let neg = NonZeroI128::new(-1)?;
let min = NonZeroI128::new(i128::MIN)?;
assert_eq!((pos, false), pos.overflowing_abs());
assert_eq!((pos, false), neg.overflowing_abs());
assert_eq!((min, true), min.overflowing_abs());
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_abs(self) -> Self
pub const fn saturating_abs(self) -> Self
Saturating absolute value, see
i128::saturating_abs
.
§Example
let pos = NonZeroI128::new(1)?;
let neg = NonZeroI128::new(-1)?;
let min = NonZeroI128::new(i128::MIN)?;
let min_plus = NonZeroI128::new(i128::MIN + 1)?;
let max = NonZeroI128::new(i128::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());
Run1.64.0 (const: 1.64.0) · sourcepub const fn wrapping_abs(self) -> Self
pub const fn wrapping_abs(self) -> Self
Wrapping absolute value, see
i128::wrapping_abs
.
§Example
let pos = NonZeroI128::new(1)?;
let neg = NonZeroI128::new(-1)?;
let min = NonZeroI128::new(i128::MIN)?;
assert_eq!(pos, pos.wrapping_abs());
assert_eq!(pos, neg.wrapping_abs());
assert_eq!(min, min.wrapping_abs());
assert_eq!(max, (-max).wrapping_abs());
Run1.64.0 (const: 1.64.0) · sourcepub const fn unsigned_abs(self) -> NonZeroU128
pub const fn unsigned_abs(self) -> NonZeroU128
Computes the absolute value of self without any wrapping or panicking.
§Example
let u_pos = NonZeroU128::new(1)?;
let i_pos = NonZeroI128::new(1)?;
let i_neg = NonZeroI128::new(-1)?;
let i_min = NonZeroI128::new(i128::MIN)?;
let u_max = NonZeroU128::new(u128::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());
Run1.71.0 (const: 1.71.0) · sourcepub const fn is_positive(self) -> bool
pub const fn is_positive(self) -> bool
1.71.0 (const: 1.71.0) · sourcepub const fn is_negative(self) -> bool
pub const fn is_negative(self) -> bool
1.71.0 (const: 1.71.0) · sourcepub const fn checked_neg(self) -> Option<Self>
pub const fn checked_neg(self) -> Option<Self>
1.71.0 (const: 1.71.0) · sourcepub const fn overflowing_neg(self) -> (Self, bool)
pub const fn overflowing_neg(self) -> (Self, bool)
Negates self, overflowing if this is equal to the minimum value.
See i128::overflowing_neg
for documentation on overflow behaviour.
§Example
let pos_five = NonZeroI128::new(5)?;
let neg_five = NonZeroI128::new(-5)?;
let min = NonZeroI128::new(i128::MIN)?;
assert_eq!(pos_five.overflowing_neg(), (neg_five, false));
assert_eq!(min.overflowing_neg(), (min, true));
Run1.71.0 (const: 1.71.0) · sourcepub const fn saturating_neg(self) -> Self
pub const fn saturating_neg(self) -> Self
Saturating negation. Computes -self
,
returning NonZeroI128::MAX
if self == NonZeroI128::MIN
instead of overflowing.
§Example
let pos_five = NonZeroI128::new(5)?;
let neg_five = NonZeroI128::new(-5)?;
let min = NonZeroI128::new(i128::MIN)?;
let min_plus_one = NonZeroI128::new(i128::MIN + 1)?;
let max = NonZeroI128::new(i128::MAX)?;
assert_eq!(pos_five.saturating_neg(), neg_five);
assert_eq!(min.saturating_neg(), max);
assert_eq!(max.saturating_neg(), min_plus_one);
Run1.71.0 (const: 1.71.0) · sourcepub const fn wrapping_neg(self) -> Self
pub const fn wrapping_neg(self) -> Self
Wrapping (modular) negation. Computes -self
, wrapping around at the boundary
of the type.
See i128::wrapping_neg
for documentation on overflow behaviour.
§Example
let pos_five = NonZeroI128::new(5)?;
let neg_five = NonZeroI128::new(-5)?;
let min = NonZeroI128::new(i128::MIN)?;
assert_eq!(pos_five.wrapping_neg(), neg_five);
assert_eq!(min.wrapping_neg(), min);
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_mul(self, other: Self) -> Option<Self>
pub const fn checked_mul(self, other: Self) -> Option<Self>
Multiplies two non-zero integers together.
Checks for overflow and returns None
on overflow.
As a consequence, the result cannot wrap to zero.
§Examples
let two = NonZeroI128::new(2)?;
let four = NonZeroI128::new(4)?;
let max = NonZeroI128::new(i128::MAX)?;
assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_mul(self, other: Self) -> Self
pub const fn saturating_mul(self, other: Self) -> Self
Multiplies two non-zero integers together.
Return NonZeroI128::MAX
on overflow.
§Examples
let two = NonZeroI128::new(2)?;
let four = NonZeroI128::new(4)?;
let max = NonZeroI128::new(i128::MAX)?;
assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));
Runsourcepub const unsafe fn unchecked_mul(self, other: Self) -> Self
🔬This is a nightly-only experimental API. (nonzero_ops
#84186)
pub const unsafe fn unchecked_mul(self, other: Self) -> Self
nonzero_ops
#84186)Multiplies two non-zero integers together,
assuming overflow cannot occur.
Overflow is unchecked, and it is undefined behaviour to overflow
even if the result would wrap to a non-zero value.
The behaviour is undefined as soon as
self * rhs > i128::MAX
, or self * rhs < i128::MIN
.
§Examples
#![feature(nonzero_ops)]
let two = NonZeroI128::new(2)?;
let four = NonZeroI128::new(4)?;
assert_eq!(four, unsafe { two.unchecked_mul(two) });
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_pow(self, other: u32) -> Option<Self>
pub const fn checked_pow(self, other: u32) -> Option<Self>
Raises non-zero value to an integer power.
Checks for overflow and returns None
on overflow.
As a consequence, the result cannot wrap to zero.
§Examples
let three = NonZeroI128::new(3)?;
let twenty_seven = NonZeroI128::new(27)?;
let half_max = NonZeroI128::new(i128::MAX / 2)?;
assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_pow(self, other: u32) -> Self
pub const fn saturating_pow(self, other: u32) -> Self
Raise non-zero value to an integer power.
Return NonZeroI128::MIN
or NonZeroI128::MAX
on overflow.
§Examples
let three = NonZeroI128::new(3)?;
let twenty_seven = NonZeroI128::new(27)?;
let max = NonZeroI128::new(i128::MAX)?;
assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));
Runsource§impl NonZero<isize>
impl NonZero<isize>
1.67.0 · sourcepub const BITS: u32 = 64u32
pub const BITS: u32 = 64u32
The size of this non-zero integer type in bits.
This value is equal to isize::BITS
.
§Examples
assert_eq!(NonZeroIsize::BITS, isize::BITS);
Run1.53.0 (const: 1.53.0) · sourcepub const fn leading_zeros(self) -> u32
pub const fn leading_zeros(self) -> u32
Returns the number of leading zeros in the binary representation of self
.
On many architectures, this function can perform better than leading_zeros()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
let n = std::num::NonZeroIsize::new(-1isize).unwrap();
assert_eq!(n.leading_zeros(), 0);
Run1.53.0 (const: 1.53.0) · sourcepub const fn trailing_zeros(self) -> u32
pub const fn trailing_zeros(self) -> u32
Returns the number of trailing zeros in the binary representation
of self
.
On many architectures, this function can perform better than trailing_zeros()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
let n = std::num::NonZeroIsize::new(0b0101000).unwrap();
assert_eq!(n.trailing_zeros(), 3);
Runconst: unstable · sourcepub fn count_ones(self) -> NonZero<u32>
🔬This is a nightly-only experimental API. (non_zero_count_ones
#120287)
pub fn count_ones(self) -> NonZero<u32>
non_zero_count_ones
#120287)Returns the number of ones in the binary representation of self
.
§Examples
Basic usage:
#![feature(generic_nonzero, non_zero_count_ones)]
let a = NonZero::<isize>::new(0b100_0000)?;
let b = NonZero::<isize>::new(0b100_0011)?;
assert_eq!(a.count_ones(), NonZero::new(1)?);
assert_eq!(b.count_ones(), NonZero::new(3)?);
Run1.70.0 · sourcepub const MIN: Self = _
pub const MIN: Self = _
The smallest value that can be represented by this non-zero
integer type,
equal to isize::MIN
.
Note: While most integer types are defined for every whole
number between MIN
and MAX
, signed non-zero integers are
a special case. They have a “gap” at 0.
§Examples
assert_eq!(NonZeroIsize::MIN.get(), isize::MIN);
Run1.70.0 · sourcepub const MAX: Self = _
pub const MAX: Self = _
The largest value that can be represented by this non-zero
integer type,
equal to isize::MAX
.
Note: While most integer types are defined for every whole
number between MIN
and MAX
, signed non-zero integers are
a special case. They have a “gap” at 0.
§Examples
assert_eq!(NonZeroIsize::MAX.get(), isize::MAX);
Run1.64.0 (const: 1.64.0) · sourcepub const fn abs(self) -> Self
pub const fn abs(self) -> Self
Computes the absolute value of self.
See isize::abs
for documentation on overflow behaviour.
§Example
let pos = NonZeroIsize::new(1)?;
let neg = NonZeroIsize::new(-1)?;
assert_eq!(pos, pos.abs());
assert_eq!(pos, neg.abs());
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_abs(self) -> Option<Self>
pub const fn checked_abs(self) -> Option<Self>
Checked absolute value.
Checks for overflow and returns None
if
self == NonZeroIsize::MIN
.
The result cannot be zero.
§Example
let pos = NonZeroIsize::new(1)?;
let neg = NonZeroIsize::new(-1)?;
let min = NonZeroIsize::new(isize::MIN)?;
assert_eq!(Some(pos), neg.checked_abs());
assert_eq!(None, min.checked_abs());
Run1.64.0 (const: 1.64.0) · sourcepub const fn overflowing_abs(self) -> (Self, bool)
pub const fn overflowing_abs(self) -> (Self, bool)
Computes the absolute value of self,
with overflow information, see
isize::overflowing_abs
.
§Example
let pos = NonZeroIsize::new(1)?;
let neg = NonZeroIsize::new(-1)?;
let min = NonZeroIsize::new(isize::MIN)?;
assert_eq!((pos, false), pos.overflowing_abs());
assert_eq!((pos, false), neg.overflowing_abs());
assert_eq!((min, true), min.overflowing_abs());
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_abs(self) -> Self
pub const fn saturating_abs(self) -> Self
Saturating absolute value, see
isize::saturating_abs
.
§Example
let pos = NonZeroIsize::new(1)?;
let neg = NonZeroIsize::new(-1)?;
let min = NonZeroIsize::new(isize::MIN)?;
let min_plus = NonZeroIsize::new(isize::MIN + 1)?;
let max = NonZeroIsize::new(isize::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());
Run1.64.0 (const: 1.64.0) · sourcepub const fn wrapping_abs(self) -> Self
pub const fn wrapping_abs(self) -> Self
Wrapping absolute value, see
isize::wrapping_abs
.
§Example
let pos = NonZeroIsize::new(1)?;
let neg = NonZeroIsize::new(-1)?;
let min = NonZeroIsize::new(isize::MIN)?;
assert_eq!(pos, pos.wrapping_abs());
assert_eq!(pos, neg.wrapping_abs());
assert_eq!(min, min.wrapping_abs());
assert_eq!(max, (-max).wrapping_abs());
Run1.64.0 (const: 1.64.0) · sourcepub const fn unsigned_abs(self) -> NonZeroUsize
pub const fn unsigned_abs(self) -> NonZeroUsize
Computes the absolute value of self without any wrapping or panicking.
§Example
let u_pos = NonZeroUsize::new(1)?;
let i_pos = NonZeroIsize::new(1)?;
let i_neg = NonZeroIsize::new(-1)?;
let i_min = NonZeroIsize::new(isize::MIN)?;
let u_max = NonZeroUsize::new(usize::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());
Run1.71.0 (const: 1.71.0) · sourcepub const fn is_positive(self) -> bool
pub const fn is_positive(self) -> bool
1.71.0 (const: 1.71.0) · sourcepub const fn is_negative(self) -> bool
pub const fn is_negative(self) -> bool
1.71.0 (const: 1.71.0) · sourcepub const fn checked_neg(self) -> Option<Self>
pub const fn checked_neg(self) -> Option<Self>
1.71.0 (const: 1.71.0) · sourcepub const fn overflowing_neg(self) -> (Self, bool)
pub const fn overflowing_neg(self) -> (Self, bool)
Negates self, overflowing if this is equal to the minimum value.
See isize::overflowing_neg
for documentation on overflow behaviour.
§Example
let pos_five = NonZeroIsize::new(5)?;
let neg_five = NonZeroIsize::new(-5)?;
let min = NonZeroIsize::new(isize::MIN)?;
assert_eq!(pos_five.overflowing_neg(), (neg_five, false));
assert_eq!(min.overflowing_neg(), (min, true));
Run1.71.0 (const: 1.71.0) · sourcepub const fn saturating_neg(self) -> Self
pub const fn saturating_neg(self) -> Self
Saturating negation. Computes -self
,
returning NonZeroIsize::MAX
if self == NonZeroIsize::MIN
instead of overflowing.
§Example
let pos_five = NonZeroIsize::new(5)?;
let neg_five = NonZeroIsize::new(-5)?;
let min = NonZeroIsize::new(isize::MIN)?;
let min_plus_one = NonZeroIsize::new(isize::MIN + 1)?;
let max = NonZeroIsize::new(isize::MAX)?;
assert_eq!(pos_five.saturating_neg(), neg_five);
assert_eq!(min.saturating_neg(), max);
assert_eq!(max.saturating_neg(), min_plus_one);
Run1.71.0 (const: 1.71.0) · sourcepub const fn wrapping_neg(self) -> Self
pub const fn wrapping_neg(self) -> Self
Wrapping (modular) negation. Computes -self
, wrapping around at the boundary
of the type.
See isize::wrapping_neg
for documentation on overflow behaviour.
§Example
let pos_five = NonZeroIsize::new(5)?;
let neg_five = NonZeroIsize::new(-5)?;
let min = NonZeroIsize::new(isize::MIN)?;
assert_eq!(pos_five.wrapping_neg(), neg_five);
assert_eq!(min.wrapping_neg(), min);
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_mul(self, other: Self) -> Option<Self>
pub const fn checked_mul(self, other: Self) -> Option<Self>
Multiplies two non-zero integers together.
Checks for overflow and returns None
on overflow.
As a consequence, the result cannot wrap to zero.
§Examples
let two = NonZeroIsize::new(2)?;
let four = NonZeroIsize::new(4)?;
let max = NonZeroIsize::new(isize::MAX)?;
assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_mul(self, other: Self) -> Self
pub const fn saturating_mul(self, other: Self) -> Self
Multiplies two non-zero integers together.
Return NonZeroIsize::MAX
on overflow.
§Examples
let two = NonZeroIsize::new(2)?;
let four = NonZeroIsize::new(4)?;
let max = NonZeroIsize::new(isize::MAX)?;
assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));
Runsourcepub const unsafe fn unchecked_mul(self, other: Self) -> Self
🔬This is a nightly-only experimental API. (nonzero_ops
#84186)
pub const unsafe fn unchecked_mul(self, other: Self) -> Self
nonzero_ops
#84186)Multiplies two non-zero integers together,
assuming overflow cannot occur.
Overflow is unchecked, and it is undefined behaviour to overflow
even if the result would wrap to a non-zero value.
The behaviour is undefined as soon as
self * rhs > isize::MAX
, or self * rhs < isize::MIN
.
§Examples
#![feature(nonzero_ops)]
let two = NonZeroIsize::new(2)?;
let four = NonZeroIsize::new(4)?;
assert_eq!(four, unsafe { two.unchecked_mul(two) });
Run1.64.0 (const: 1.64.0) · sourcepub const fn checked_pow(self, other: u32) -> Option<Self>
pub const fn checked_pow(self, other: u32) -> Option<Self>
Raises non-zero value to an integer power.
Checks for overflow and returns None
on overflow.
As a consequence, the result cannot wrap to zero.
§Examples
let three = NonZeroIsize::new(3)?;
let twenty_seven = NonZeroIsize::new(27)?;
let half_max = NonZeroIsize::new(isize::MAX / 2)?;
assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));
Run1.64.0 (const: 1.64.0) · sourcepub const fn saturating_pow(self, other: u32) -> Self
pub const fn saturating_pow(self, other: u32) -> Self
Raise non-zero value to an integer power.
Return NonZeroIsize::MIN
or NonZeroIsize::MAX
on overflow.
§Examples
let three = NonZeroIsize::new(3)?;
let twenty_seven = NonZeroIsize::new(27)?;
let max = NonZeroIsize::new(isize::MAX)?;
assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));
RunTrait Implementations§
1.45.0 · source§impl<T> BitOrAssign<T> for NonZero<T>where
T: ZeroablePrimitive,
Self: BitOr<T, Output = Self>,
impl<T> BitOrAssign<T> for NonZero<T>where
T: ZeroablePrimitive,
Self: BitOr<T, Output = Self>,
source§fn bitor_assign(&mut self, rhs: T)
fn bitor_assign(&mut self, rhs: T)
|=
operation. Read more1.45.0 · source§impl<T> BitOrAssign for NonZero<T>where
T: ZeroablePrimitive,
Self: BitOr<Output = Self>,
impl<T> BitOrAssign for NonZero<T>where
T: ZeroablePrimitive,
Self: BitOr<Output = Self>,
source§fn bitor_assign(&mut self, rhs: Self)
fn bitor_assign(&mut self, rhs: Self)
|=
operation. Read more