pub struct NonZero<T>(/* private fields */)
where
T: ZeroablePrimitive;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:
use core::{mem::size_of, num::NonZero};
assert_eq!(size_of::<Option<NonZero<u32>>>(), size_of::<u32>());Implementationsยง
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<NonZero<T>>
pub const fn new(n: T) -> Option<NonZero<T>>
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) -> NonZero<T>
pub const unsafe fn new_unchecked(n: T) -> NonZero<T>
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 NonZero<T>>
๐ฌThis is a nightly-only experimental API. (nonzero_from_mut)
pub fn from_mut(n: &mut T) -> Option<&mut NonZero<T>>
nonzero_from_mut)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 NonZero<T>
๐ฌThis is a nightly-only experimental API. (nonzero_from_mut)
pub unsafe fn from_mut_unchecked(n: &mut T) -> &mut NonZero<T>
nonzero_from_mut)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 = NonZero::<u8>::new(u8::MAX)?;
assert_eq!(n.leading_zeros(), 0);1.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 = NonZero::<u8>::new(0b0101000)?;
assert_eq!(n.trailing_zeros(), 3);sourcepub const fn count_ones(self) -> NonZero<u32>
๐ฌThis is a nightly-only experimental API. (non_zero_count_ones)
pub const fn count_ones(self) -> NonZero<u32>
non_zero_count_ones)Returns the number of ones in the binary representation of self.
ยงExamples
Basic usage:
#![feature(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)?);1.70.0 ยท sourcepub const MIN: NonZero<u8> = _
pub const MIN: NonZero<u8> = _
The smallest value that can be represented by this non-zero integer type, 1.
ยงExamples
assert_eq!(NonZero::<u8>::MIN.get(), 1u8);1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_add(self, other: u8) -> Option<NonZero<u8>>
pub const fn checked_add(self, other: u8) -> Option<NonZero<u8>>
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 = NonZero::new(1u8)?;
let two = NonZero::new(2u8)?;
let max = NonZero::new(u8::MAX)?;
assert_eq!(Some(two), one.checked_add(1));
assert_eq!(None, max.checked_add(1));1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_add(self, other: u8) -> NonZero<u8>
pub const fn saturating_add(self, other: u8) -> NonZero<u8>
Adds an unsigned integer to a non-zero value.
Return NonZero::<u8>::MAX on overflow.
ยงExamples
let one = NonZero::new(1u8)?;
let two = NonZero::new(2u8)?;
let max = NonZero::new(u8::MAX)?;
assert_eq!(two, one.saturating_add(1));
assert_eq!(max, max.saturating_add(1));sourcepub const unsafe fn unchecked_add(self, other: u8) -> NonZero<u8>
๐ฌThis is a nightly-only experimental API. (nonzero_ops)
pub const unsafe fn unchecked_add(self, other: u8) -> NonZero<u8>
nonzero_ops)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 = NonZero::new(1u8)?;
let two = NonZero::new(2u8)?;
assert_eq!(two, unsafe { one.unchecked_add(1) });1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_next_power_of_two(self) -> Option<NonZero<u8>>
pub const fn checked_next_power_of_two(self) -> Option<NonZero<u8>>
Returns the smallest power of two greater than or equal to self.
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 = NonZero::new(2u8)?;
let three = NonZero::new(3u8)?;
let four = NonZero::new(4u8)?;
let max = NonZero::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() );1.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!(NonZero::new(7u8)?.ilog2(), 2);
assert_eq!(NonZero::new(8u8)?.ilog2(), 3);
assert_eq!(NonZero::new(9u8)?.ilog2(), 3);1.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!(NonZero::new(99u8)?.ilog10(), 1);
assert_eq!(NonZero::new(100u8)?.ilog10(), 2);
assert_eq!(NonZero::new(101u8)?.ilog10(), 2);sourcepub const fn midpoint(self, rhs: NonZero<u8>) -> NonZero<u8>
๐ฌThis is a nightly-only experimental API. (num_midpoint)
pub const fn midpoint(self, rhs: NonZero<u8>) -> NonZero<u8>
num_midpoint)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 = NonZero::new(1u8)?;
let two = NonZero::new(2u8)?;
let four = NonZero::new(4u8)?;
assert_eq!(one.midpoint(four), two);
assert_eq!(four.midpoint(one), two);1.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 = NonZero::new(8u8)?;
assert!(eight.is_power_of_two());
let ten = NonZero::new(10u8)?;
assert!(!ten.is_power_of_two());sourcepub const fn isqrt(self) -> NonZero<u8>
๐ฌThis is a nightly-only experimental API. (isqrt)
pub const fn isqrt(self) -> NonZero<u8>
isqrt)Returns the square root of the number, rounded down.
ยงExamples
Basic usage:
#![feature(isqrt)]
let ten = NonZero::new(10u8)?;
let three = NonZero::new(3u8)?;
assert_eq!(ten.isqrt(), three);1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_mul(self, other: NonZero<u8>) -> Option<NonZero<u8>>
pub const fn checked_mul(self, other: NonZero<u8>) -> Option<NonZero<u8>>
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 = NonZero::new(2u8)?;
let four = NonZero::new(4u8)?;
let max = NonZero::new(u8::MAX)?;
assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_mul(self, other: NonZero<u8>) -> NonZero<u8>
pub const fn saturating_mul(self, other: NonZero<u8>) -> NonZero<u8>
Multiplies two non-zero integers together.
Return NonZero::<u8>::MAX on overflow.
ยงExamples
let two = NonZero::new(2u8)?;
let four = NonZero::new(4u8)?;
let max = NonZero::new(u8::MAX)?;
assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));sourcepub const unsafe fn unchecked_mul(self, other: NonZero<u8>) -> NonZero<u8>
๐ฌThis is a nightly-only experimental API. (nonzero_ops)
pub const unsafe fn unchecked_mul(self, other: NonZero<u8>) -> NonZero<u8>
nonzero_ops)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 = NonZero::new(2u8)?;
let four = NonZero::new(4u8)?;
assert_eq!(four, unsafe { two.unchecked_mul(two) });1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_pow(self, other: u32) -> Option<NonZero<u8>>
pub const fn checked_pow(self, other: u32) -> Option<NonZero<u8>>
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 = NonZero::new(3u8)?;
let twenty_seven = NonZero::new(27u8)?;
let half_max = NonZero::new(u8::MAX / 2)?;
assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_pow(self, other: u32) -> NonZero<u8>
pub const fn saturating_pow(self, other: u32) -> NonZero<u8>
Raise non-zero value to an integer power.
Return NonZero::<u8>::MAX on overflow.
ยงExamples
let three = NonZero::new(3u8)?;
let twenty_seven = NonZero::new(27u8)?;
let max = NonZero::new(u8::MAX)?;
assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));sourceยง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 = NonZero::<u16>::new(u16::MAX)?;
assert_eq!(n.leading_zeros(), 0);1.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 = NonZero::<u16>::new(0b0101000)?;
assert_eq!(n.trailing_zeros(), 3);sourcepub const fn count_ones(self) -> NonZero<u32>
๐ฌThis is a nightly-only experimental API. (non_zero_count_ones)
pub const fn count_ones(self) -> NonZero<u32>
non_zero_count_ones)Returns the number of ones in the binary representation of self.
ยงExamples
Basic usage:
#![feature(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)?);1.70.0 ยท sourcepub const MIN: NonZero<u16> = _
pub const MIN: NonZero<u16> = _
The smallest value that can be represented by this non-zero integer type, 1.
ยงExamples
assert_eq!(NonZero::<u16>::MIN.get(), 1u16);1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_add(self, other: u16) -> Option<NonZero<u16>>
pub const fn checked_add(self, other: u16) -> Option<NonZero<u16>>
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 = NonZero::new(1u16)?;
let two = NonZero::new(2u16)?;
let max = NonZero::new(u16::MAX)?;
assert_eq!(Some(two), one.checked_add(1));
assert_eq!(None, max.checked_add(1));1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_add(self, other: u16) -> NonZero<u16>
pub const fn saturating_add(self, other: u16) -> NonZero<u16>
Adds an unsigned integer to a non-zero value.
Return NonZero::<u16>::MAX on overflow.
ยงExamples
let one = NonZero::new(1u16)?;
let two = NonZero::new(2u16)?;
let max = NonZero::new(u16::MAX)?;
assert_eq!(two, one.saturating_add(1));
assert_eq!(max, max.saturating_add(1));sourcepub const unsafe fn unchecked_add(self, other: u16) -> NonZero<u16>
๐ฌThis is a nightly-only experimental API. (nonzero_ops)
pub const unsafe fn unchecked_add(self, other: u16) -> NonZero<u16>
nonzero_ops)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 = NonZero::new(1u16)?;
let two = NonZero::new(2u16)?;
assert_eq!(two, unsafe { one.unchecked_add(1) });1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_next_power_of_two(self) -> Option<NonZero<u16>>
pub const fn checked_next_power_of_two(self) -> Option<NonZero<u16>>
Returns the smallest power of two greater than or equal to self.
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 = NonZero::new(2u16)?;
let three = NonZero::new(3u16)?;
let four = NonZero::new(4u16)?;
let max = NonZero::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() );1.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!(NonZero::new(7u16)?.ilog2(), 2);
assert_eq!(NonZero::new(8u16)?.ilog2(), 3);
assert_eq!(NonZero::new(9u16)?.ilog2(), 3);1.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!(NonZero::new(99u16)?.ilog10(), 1);
assert_eq!(NonZero::new(100u16)?.ilog10(), 2);
assert_eq!(NonZero::new(101u16)?.ilog10(), 2);sourcepub const fn midpoint(self, rhs: NonZero<u16>) -> NonZero<u16>
๐ฌThis is a nightly-only experimental API. (num_midpoint)
pub const fn midpoint(self, rhs: NonZero<u16>) -> NonZero<u16>
num_midpoint)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 = NonZero::new(1u16)?;
let two = NonZero::new(2u16)?;
let four = NonZero::new(4u16)?;
assert_eq!(one.midpoint(four), two);
assert_eq!(four.midpoint(one), two);1.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 = NonZero::new(8u16)?;
assert!(eight.is_power_of_two());
let ten = NonZero::new(10u16)?;
assert!(!ten.is_power_of_two());sourcepub const fn isqrt(self) -> NonZero<u16>
๐ฌThis is a nightly-only experimental API. (isqrt)
pub const fn isqrt(self) -> NonZero<u16>
isqrt)Returns the square root of the number, rounded down.
ยงExamples
Basic usage:
#![feature(isqrt)]
let ten = NonZero::new(10u16)?;
let three = NonZero::new(3u16)?;
assert_eq!(ten.isqrt(), three);1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_mul(self, other: NonZero<u16>) -> Option<NonZero<u16>>
pub const fn checked_mul(self, other: NonZero<u16>) -> Option<NonZero<u16>>
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 = NonZero::new(2u16)?;
let four = NonZero::new(4u16)?;
let max = NonZero::new(u16::MAX)?;
assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_mul(self, other: NonZero<u16>) -> NonZero<u16>
pub const fn saturating_mul(self, other: NonZero<u16>) -> NonZero<u16>
Multiplies two non-zero integers together.
Return NonZero::<u16>::MAX on overflow.
ยงExamples
let two = NonZero::new(2u16)?;
let four = NonZero::new(4u16)?;
let max = NonZero::new(u16::MAX)?;
assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));sourcepub const unsafe fn unchecked_mul(self, other: NonZero<u16>) -> NonZero<u16>
๐ฌThis is a nightly-only experimental API. (nonzero_ops)
pub const unsafe fn unchecked_mul(self, other: NonZero<u16>) -> NonZero<u16>
nonzero_ops)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 = NonZero::new(2u16)?;
let four = NonZero::new(4u16)?;
assert_eq!(four, unsafe { two.unchecked_mul(two) });1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_pow(self, other: u32) -> Option<NonZero<u16>>
pub const fn checked_pow(self, other: u32) -> Option<NonZero<u16>>
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 = NonZero::new(3u16)?;
let twenty_seven = NonZero::new(27u16)?;
let half_max = NonZero::new(u16::MAX / 2)?;
assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_pow(self, other: u32) -> NonZero<u16>
pub const fn saturating_pow(self, other: u32) -> NonZero<u16>
Raise non-zero value to an integer power.
Return NonZero::<u16>::MAX on overflow.
ยงExamples
let three = NonZero::new(3u16)?;
let twenty_seven = NonZero::new(27u16)?;
let max = NonZero::new(u16::MAX)?;
assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));sourceยง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 = NonZero::<u32>::new(u32::MAX)?;
assert_eq!(n.leading_zeros(), 0);1.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 = NonZero::<u32>::new(0b0101000)?;
assert_eq!(n.trailing_zeros(), 3);sourcepub const fn count_ones(self) -> NonZero<u32>
๐ฌThis is a nightly-only experimental API. (non_zero_count_ones)
pub const fn count_ones(self) -> NonZero<u32>
non_zero_count_ones)Returns the number of ones in the binary representation of self.
ยงExamples
Basic usage:
#![feature(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)?);1.70.0 ยท sourcepub const MIN: NonZero<u32> = _
pub const MIN: NonZero<u32> = _
The smallest value that can be represented by this non-zero integer type, 1.
ยงExamples
assert_eq!(NonZero::<u32>::MIN.get(), 1u32);1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_add(self, other: u32) -> Option<NonZero<u32>>
pub const fn checked_add(self, other: u32) -> Option<NonZero<u32>>
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 = NonZero::new(1u32)?;
let two = NonZero::new(2u32)?;
let max = NonZero::new(u32::MAX)?;
assert_eq!(Some(two), one.checked_add(1));
assert_eq!(None, max.checked_add(1));1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_add(self, other: u32) -> NonZero<u32>
pub const fn saturating_add(self, other: u32) -> NonZero<u32>
Adds an unsigned integer to a non-zero value.
Return NonZero::<u32>::MAX on overflow.
ยงExamples
let one = NonZero::new(1u32)?;
let two = NonZero::new(2u32)?;
let max = NonZero::new(u32::MAX)?;
assert_eq!(two, one.saturating_add(1));
assert_eq!(max, max.saturating_add(1));sourcepub const unsafe fn unchecked_add(self, other: u32) -> NonZero<u32>
๐ฌThis is a nightly-only experimental API. (nonzero_ops)
pub const unsafe fn unchecked_add(self, other: u32) -> NonZero<u32>
nonzero_ops)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 = NonZero::new(1u32)?;
let two = NonZero::new(2u32)?;
assert_eq!(two, unsafe { one.unchecked_add(1) });1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_next_power_of_two(self) -> Option<NonZero<u32>>
pub const fn checked_next_power_of_two(self) -> Option<NonZero<u32>>
Returns the smallest power of two greater than or equal to self.
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 = NonZero::new(2u32)?;
let three = NonZero::new(3u32)?;
let four = NonZero::new(4u32)?;
let max = NonZero::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() );1.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!(NonZero::new(7u32)?.ilog2(), 2);
assert_eq!(NonZero::new(8u32)?.ilog2(), 3);
assert_eq!(NonZero::new(9u32)?.ilog2(), 3);1.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!(NonZero::new(99u32)?.ilog10(), 1);
assert_eq!(NonZero::new(100u32)?.ilog10(), 2);
assert_eq!(NonZero::new(101u32)?.ilog10(), 2);sourcepub const fn midpoint(self, rhs: NonZero<u32>) -> NonZero<u32>
๐ฌThis is a nightly-only experimental API. (num_midpoint)
pub const fn midpoint(self, rhs: NonZero<u32>) -> NonZero<u32>
num_midpoint)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 = NonZero::new(1u32)?;
let two = NonZero::new(2u32)?;
let four = NonZero::new(4u32)?;
assert_eq!(one.midpoint(four), two);
assert_eq!(four.midpoint(one), two);1.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 = NonZero::new(8u32)?;
assert!(eight.is_power_of_two());
let ten = NonZero::new(10u32)?;
assert!(!ten.is_power_of_two());sourcepub const fn isqrt(self) -> NonZero<u32>
๐ฌThis is a nightly-only experimental API. (isqrt)
pub const fn isqrt(self) -> NonZero<u32>
isqrt)Returns the square root of the number, rounded down.
ยงExamples
Basic usage:
#![feature(isqrt)]
let ten = NonZero::new(10u32)?;
let three = NonZero::new(3u32)?;
assert_eq!(ten.isqrt(), three);1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_mul(self, other: NonZero<u32>) -> Option<NonZero<u32>>
pub const fn checked_mul(self, other: NonZero<u32>) -> Option<NonZero<u32>>
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 = NonZero::new(2u32)?;
let four = NonZero::new(4u32)?;
let max = NonZero::new(u32::MAX)?;
assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_mul(self, other: NonZero<u32>) -> NonZero<u32>
pub const fn saturating_mul(self, other: NonZero<u32>) -> NonZero<u32>
Multiplies two non-zero integers together.
Return NonZero::<u32>::MAX on overflow.
ยงExamples
let two = NonZero::new(2u32)?;
let four = NonZero::new(4u32)?;
let max = NonZero::new(u32::MAX)?;
assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));sourcepub const unsafe fn unchecked_mul(self, other: NonZero<u32>) -> NonZero<u32>
๐ฌThis is a nightly-only experimental API. (nonzero_ops)
pub const unsafe fn unchecked_mul(self, other: NonZero<u32>) -> NonZero<u32>
nonzero_ops)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 = NonZero::new(2u32)?;
let four = NonZero::new(4u32)?;
assert_eq!(four, unsafe { two.unchecked_mul(two) });1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_pow(self, other: u32) -> Option<NonZero<u32>>
pub const fn checked_pow(self, other: u32) -> Option<NonZero<u32>>
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 = NonZero::new(3u32)?;
let twenty_seven = NonZero::new(27u32)?;
let half_max = NonZero::new(u32::MAX / 2)?;
assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_pow(self, other: u32) -> NonZero<u32>
pub const fn saturating_pow(self, other: u32) -> NonZero<u32>
Raise non-zero value to an integer power.
Return NonZero::<u32>::MAX on overflow.
ยงExamples
let three = NonZero::new(3u32)?;
let twenty_seven = NonZero::new(27u32)?;
let max = NonZero::new(u32::MAX)?;
assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));sourceยง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 = NonZero::<u64>::new(u64::MAX)?;
assert_eq!(n.leading_zeros(), 0);1.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 = NonZero::<u64>::new(0b0101000)?;
assert_eq!(n.trailing_zeros(), 3);sourcepub const fn count_ones(self) -> NonZero<u32>
๐ฌThis is a nightly-only experimental API. (non_zero_count_ones)
pub const fn count_ones(self) -> NonZero<u32>
non_zero_count_ones)Returns the number of ones in the binary representation of self.
ยงExamples
Basic usage:
#![feature(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)?);1.70.0 ยท sourcepub const MIN: NonZero<u64> = _
pub const MIN: NonZero<u64> = _
The smallest value that can be represented by this non-zero integer type, 1.
ยงExamples
assert_eq!(NonZero::<u64>::MIN.get(), 1u64);1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_add(self, other: u64) -> Option<NonZero<u64>>
pub const fn checked_add(self, other: u64) -> Option<NonZero<u64>>
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 = NonZero::new(1u64)?;
let two = NonZero::new(2u64)?;
let max = NonZero::new(u64::MAX)?;
assert_eq!(Some(two), one.checked_add(1));
assert_eq!(None, max.checked_add(1));1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_add(self, other: u64) -> NonZero<u64>
pub const fn saturating_add(self, other: u64) -> NonZero<u64>
Adds an unsigned integer to a non-zero value.
Return NonZero::<u64>::MAX on overflow.
ยงExamples
let one = NonZero::new(1u64)?;
let two = NonZero::new(2u64)?;
let max = NonZero::new(u64::MAX)?;
assert_eq!(two, one.saturating_add(1));
assert_eq!(max, max.saturating_add(1));sourcepub const unsafe fn unchecked_add(self, other: u64) -> NonZero<u64>
๐ฌThis is a nightly-only experimental API. (nonzero_ops)
pub const unsafe fn unchecked_add(self, other: u64) -> NonZero<u64>
nonzero_ops)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 = NonZero::new(1u64)?;
let two = NonZero::new(2u64)?;
assert_eq!(two, unsafe { one.unchecked_add(1) });1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_next_power_of_two(self) -> Option<NonZero<u64>>
pub const fn checked_next_power_of_two(self) -> Option<NonZero<u64>>
Returns the smallest power of two greater than or equal to self.
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 = NonZero::new(2u64)?;
let three = NonZero::new(3u64)?;
let four = NonZero::new(4u64)?;
let max = NonZero::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() );1.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!(NonZero::new(7u64)?.ilog2(), 2);
assert_eq!(NonZero::new(8u64)?.ilog2(), 3);
assert_eq!(NonZero::new(9u64)?.ilog2(), 3);1.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!(NonZero::new(99u64)?.ilog10(), 1);
assert_eq!(NonZero::new(100u64)?.ilog10(), 2);
assert_eq!(NonZero::new(101u64)?.ilog10(), 2);sourcepub const fn midpoint(self, rhs: NonZero<u64>) -> NonZero<u64>
๐ฌThis is a nightly-only experimental API. (num_midpoint)
pub const fn midpoint(self, rhs: NonZero<u64>) -> NonZero<u64>
num_midpoint)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 = NonZero::new(1u64)?;
let two = NonZero::new(2u64)?;
let four = NonZero::new(4u64)?;
assert_eq!(one.midpoint(four), two);
assert_eq!(four.midpoint(one), two);1.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 = NonZero::new(8u64)?;
assert!(eight.is_power_of_two());
let ten = NonZero::new(10u64)?;
assert!(!ten.is_power_of_two());sourcepub const fn isqrt(self) -> NonZero<u64>
๐ฌThis is a nightly-only experimental API. (isqrt)
pub const fn isqrt(self) -> NonZero<u64>
isqrt)Returns the square root of the number, rounded down.
ยงExamples
Basic usage:
#![feature(isqrt)]
let ten = NonZero::new(10u64)?;
let three = NonZero::new(3u64)?;
assert_eq!(ten.isqrt(), three);1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_mul(self, other: NonZero<u64>) -> Option<NonZero<u64>>
pub const fn checked_mul(self, other: NonZero<u64>) -> Option<NonZero<u64>>
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 = NonZero::new(2u64)?;
let four = NonZero::new(4u64)?;
let max = NonZero::new(u64::MAX)?;
assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_mul(self, other: NonZero<u64>) -> NonZero<u64>
pub const fn saturating_mul(self, other: NonZero<u64>) -> NonZero<u64>
Multiplies two non-zero integers together.
Return NonZero::<u64>::MAX on overflow.
ยงExamples
let two = NonZero::new(2u64)?;
let four = NonZero::new(4u64)?;
let max = NonZero::new(u64::MAX)?;
assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));sourcepub const unsafe fn unchecked_mul(self, other: NonZero<u64>) -> NonZero<u64>
๐ฌThis is a nightly-only experimental API. (nonzero_ops)
pub const unsafe fn unchecked_mul(self, other: NonZero<u64>) -> NonZero<u64>
nonzero_ops)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 = NonZero::new(2u64)?;
let four = NonZero::new(4u64)?;
assert_eq!(four, unsafe { two.unchecked_mul(two) });1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_pow(self, other: u32) -> Option<NonZero<u64>>
pub const fn checked_pow(self, other: u32) -> Option<NonZero<u64>>
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 = NonZero::new(3u64)?;
let twenty_seven = NonZero::new(27u64)?;
let half_max = NonZero::new(u64::MAX / 2)?;
assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_pow(self, other: u32) -> NonZero<u64>
pub const fn saturating_pow(self, other: u32) -> NonZero<u64>
Raise non-zero value to an integer power.
Return NonZero::<u64>::MAX on overflow.
ยงExamples
let three = NonZero::new(3u64)?;
let twenty_seven = NonZero::new(27u64)?;
let max = NonZero::new(u64::MAX)?;
assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));sourceยง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!(NonZero::<u128>::BITS, u128::BITS);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 = NonZero::<u128>::new(u128::MAX)?;
assert_eq!(n.leading_zeros(), 0);1.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 = NonZero::<u128>::new(0b0101000)?;
assert_eq!(n.trailing_zeros(), 3);sourcepub const fn count_ones(self) -> NonZero<u32>
๐ฌThis is a nightly-only experimental API. (non_zero_count_ones)
pub const fn count_ones(self) -> NonZero<u32>
non_zero_count_ones)Returns the number of ones in the binary representation of self.
ยงExamples
Basic usage:
#![feature(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)?);1.70.0 ยท sourcepub const MIN: NonZero<u128> = _
pub const MIN: NonZero<u128> = _
The smallest value that can be represented by this non-zero integer type, 1.
ยงExamples
assert_eq!(NonZero::<u128>::MIN.get(), 1u128);1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_add(self, other: u128) -> Option<NonZero<u128>>
pub const fn checked_add(self, other: u128) -> Option<NonZero<u128>>
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 = NonZero::new(1u128)?;
let two = NonZero::new(2u128)?;
let max = NonZero::new(u128::MAX)?;
assert_eq!(Some(two), one.checked_add(1));
assert_eq!(None, max.checked_add(1));1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_add(self, other: u128) -> NonZero<u128>
pub const fn saturating_add(self, other: u128) -> NonZero<u128>
Adds an unsigned integer to a non-zero value.
Return NonZero::<u128>::MAX on overflow.
ยงExamples
let one = NonZero::new(1u128)?;
let two = NonZero::new(2u128)?;
let max = NonZero::new(u128::MAX)?;
assert_eq!(two, one.saturating_add(1));
assert_eq!(max, max.saturating_add(1));sourcepub const unsafe fn unchecked_add(self, other: u128) -> NonZero<u128>
๐ฌThis is a nightly-only experimental API. (nonzero_ops)
pub const unsafe fn unchecked_add(self, other: u128) -> NonZero<u128>
nonzero_ops)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 = NonZero::new(1u128)?;
let two = NonZero::new(2u128)?;
assert_eq!(two, unsafe { one.unchecked_add(1) });1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_next_power_of_two(self) -> Option<NonZero<u128>>
pub const fn checked_next_power_of_two(self) -> Option<NonZero<u128>>
Returns the smallest power of two greater than or equal to self.
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 = NonZero::new(2u128)?;
let three = NonZero::new(3u128)?;
let four = NonZero::new(4u128)?;
let max = NonZero::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() );1.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!(NonZero::new(7u128)?.ilog2(), 2);
assert_eq!(NonZero::new(8u128)?.ilog2(), 3);
assert_eq!(NonZero::new(9u128)?.ilog2(), 3);1.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!(NonZero::new(99u128)?.ilog10(), 1);
assert_eq!(NonZero::new(100u128)?.ilog10(), 2);
assert_eq!(NonZero::new(101u128)?.ilog10(), 2);sourcepub const fn midpoint(self, rhs: NonZero<u128>) -> NonZero<u128>
๐ฌThis is a nightly-only experimental API. (num_midpoint)
pub const fn midpoint(self, rhs: NonZero<u128>) -> NonZero<u128>
num_midpoint)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 = NonZero::new(1u128)?;
let two = NonZero::new(2u128)?;
let four = NonZero::new(4u128)?;
assert_eq!(one.midpoint(four), two);
assert_eq!(four.midpoint(one), two);1.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 = NonZero::new(8u128)?;
assert!(eight.is_power_of_two());
let ten = NonZero::new(10u128)?;
assert!(!ten.is_power_of_two());sourcepub const fn isqrt(self) -> NonZero<u128>
๐ฌThis is a nightly-only experimental API. (isqrt)
pub const fn isqrt(self) -> NonZero<u128>
isqrt)Returns the square root of the number, rounded down.
ยงExamples
Basic usage:
#![feature(isqrt)]
let ten = NonZero::new(10u128)?;
let three = NonZero::new(3u128)?;
assert_eq!(ten.isqrt(), three);1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_mul(self, other: NonZero<u128>) -> Option<NonZero<u128>>
pub const fn checked_mul(self, other: NonZero<u128>) -> Option<NonZero<u128>>
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 = NonZero::new(2u128)?;
let four = NonZero::new(4u128)?;
let max = NonZero::new(u128::MAX)?;
assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_mul(self, other: NonZero<u128>) -> NonZero<u128>
pub const fn saturating_mul(self, other: NonZero<u128>) -> NonZero<u128>
Multiplies two non-zero integers together.
Return NonZero::<u128>::MAX on overflow.
ยงExamples
let two = NonZero::new(2u128)?;
let four = NonZero::new(4u128)?;
let max = NonZero::new(u128::MAX)?;
assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));sourcepub const unsafe fn unchecked_mul(self, other: NonZero<u128>) -> NonZero<u128>
๐ฌThis is a nightly-only experimental API. (nonzero_ops)
pub const unsafe fn unchecked_mul(self, other: NonZero<u128>) -> NonZero<u128>
nonzero_ops)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 = NonZero::new(2u128)?;
let four = NonZero::new(4u128)?;
assert_eq!(four, unsafe { two.unchecked_mul(two) });1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_pow(self, other: u32) -> Option<NonZero<u128>>
pub const fn checked_pow(self, other: u32) -> Option<NonZero<u128>>
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 = NonZero::new(3u128)?;
let twenty_seven = NonZero::new(27u128)?;
let half_max = NonZero::new(u128::MAX / 2)?;
assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_pow(self, other: u32) -> NonZero<u128>
pub const fn saturating_pow(self, other: u32) -> NonZero<u128>
Raise non-zero value to an integer power.
Return NonZero::<u128>::MAX on overflow.
ยงExamples
let three = NonZero::new(3u128)?;
let twenty_seven = NonZero::new(27u128)?;
let max = NonZero::new(u128::MAX)?;
assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));sourceยงimpl NonZero<usize>
impl NonZero<usize>
1.67.0 ยท sourcepub const BITS: u32 = 32u32
pub const BITS: u32 = 32u32
The size of this non-zero integer type in bits.
This value is equal to usize::BITS.
ยงExamples
assert_eq!(NonZero::<usize>::BITS, usize::BITS);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 = NonZero::<usize>::new(usize::MAX)?;
assert_eq!(n.leading_zeros(), 0);1.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 = NonZero::<usize>::new(0b0101000)?;
assert_eq!(n.trailing_zeros(), 3);sourcepub const fn count_ones(self) -> NonZero<u32>
๐ฌThis is a nightly-only experimental API. (non_zero_count_ones)
pub const fn count_ones(self) -> NonZero<u32>
non_zero_count_ones)Returns the number of ones in the binary representation of self.
ยงExamples
Basic usage:
#![feature(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)?);1.70.0 ยท sourcepub const MIN: NonZero<usize> = _
pub const MIN: NonZero<usize> = _
The smallest value that can be represented by this non-zero integer type, 1.
ยงExamples
assert_eq!(NonZero::<usize>::MIN.get(), 1usize);1.70.0 ยท sourcepub const MAX: NonZero<usize> = _
pub const MAX: NonZero<usize> = _
The largest value that can be represented by this non-zero
integer type,
equal to usize::MAX.
ยงExamples
assert_eq!(NonZero::<usize>::MAX.get(), usize::MAX);1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_add(self, other: usize) -> Option<NonZero<usize>>
pub const fn checked_add(self, other: usize) -> Option<NonZero<usize>>
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 = NonZero::new(1usize)?;
let two = NonZero::new(2usize)?;
let max = NonZero::new(usize::MAX)?;
assert_eq!(Some(two), one.checked_add(1));
assert_eq!(None, max.checked_add(1));1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_add(self, other: usize) -> NonZero<usize>
pub const fn saturating_add(self, other: usize) -> NonZero<usize>
Adds an unsigned integer to a non-zero value.
Return NonZero::<usize>::MAX on overflow.
ยงExamples
let one = NonZero::new(1usize)?;
let two = NonZero::new(2usize)?;
let max = NonZero::new(usize::MAX)?;
assert_eq!(two, one.saturating_add(1));
assert_eq!(max, max.saturating_add(1));sourcepub const unsafe fn unchecked_add(self, other: usize) -> NonZero<usize>
๐ฌThis is a nightly-only experimental API. (nonzero_ops)
pub const unsafe fn unchecked_add(self, other: usize) -> NonZero<usize>
nonzero_ops)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 = NonZero::new(1usize)?;
let two = NonZero::new(2usize)?;
assert_eq!(two, unsafe { one.unchecked_add(1) });1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_next_power_of_two(self) -> Option<NonZero<usize>>
pub const fn checked_next_power_of_two(self) -> Option<NonZero<usize>>
Returns the smallest power of two greater than or equal to self.
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 = NonZero::new(2usize)?;
let three = NonZero::new(3usize)?;
let four = NonZero::new(4usize)?;
let max = NonZero::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() );1.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!(NonZero::new(7usize)?.ilog2(), 2);
assert_eq!(NonZero::new(8usize)?.ilog2(), 3);
assert_eq!(NonZero::new(9usize)?.ilog2(), 3);1.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!(NonZero::new(99usize)?.ilog10(), 1);
assert_eq!(NonZero::new(100usize)?.ilog10(), 2);
assert_eq!(NonZero::new(101usize)?.ilog10(), 2);sourcepub const fn midpoint(self, rhs: NonZero<usize>) -> NonZero<usize>
๐ฌThis is a nightly-only experimental API. (num_midpoint)
pub const fn midpoint(self, rhs: NonZero<usize>) -> NonZero<usize>
num_midpoint)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 = NonZero::new(1usize)?;
let two = NonZero::new(2usize)?;
let four = NonZero::new(4usize)?;
assert_eq!(one.midpoint(four), two);
assert_eq!(four.midpoint(one), two);1.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 = NonZero::new(8usize)?;
assert!(eight.is_power_of_two());
let ten = NonZero::new(10usize)?;
assert!(!ten.is_power_of_two());sourcepub const fn isqrt(self) -> NonZero<usize>
๐ฌThis is a nightly-only experimental API. (isqrt)
pub const fn isqrt(self) -> NonZero<usize>
isqrt)Returns the square root of the number, rounded down.
ยงExamples
Basic usage:
#![feature(isqrt)]
let ten = NonZero::new(10usize)?;
let three = NonZero::new(3usize)?;
assert_eq!(ten.isqrt(), three);1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_mul(self, other: NonZero<usize>) -> Option<NonZero<usize>>
pub const fn checked_mul(self, other: NonZero<usize>) -> Option<NonZero<usize>>
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 = NonZero::new(2usize)?;
let four = NonZero::new(4usize)?;
let max = NonZero::new(usize::MAX)?;
assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_mul(self, other: NonZero<usize>) -> NonZero<usize>
pub const fn saturating_mul(self, other: NonZero<usize>) -> NonZero<usize>
Multiplies two non-zero integers together.
Return NonZero::<usize>::MAX on overflow.
ยงExamples
let two = NonZero::new(2usize)?;
let four = NonZero::new(4usize)?;
let max = NonZero::new(usize::MAX)?;
assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));sourcepub const unsafe fn unchecked_mul(self, other: NonZero<usize>) -> NonZero<usize>
๐ฌThis is a nightly-only experimental API. (nonzero_ops)
pub const unsafe fn unchecked_mul(self, other: NonZero<usize>) -> NonZero<usize>
nonzero_ops)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 = NonZero::new(2usize)?;
let four = NonZero::new(4usize)?;
assert_eq!(four, unsafe { two.unchecked_mul(two) });1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_pow(self, other: u32) -> Option<NonZero<usize>>
pub const fn checked_pow(self, other: u32) -> Option<NonZero<usize>>
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 = NonZero::new(3usize)?;
let twenty_seven = NonZero::new(27usize)?;
let half_max = NonZero::new(usize::MAX / 2)?;
assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_pow(self, other: u32) -> NonZero<usize>
pub const fn saturating_pow(self, other: u32) -> NonZero<usize>
Raise non-zero value to an integer power.
Return NonZero::<usize>::MAX on overflow.
ยงExamples
let three = NonZero::new(3usize)?;
let twenty_seven = NonZero::new(27usize)?;
let max = NonZero::new(usize::MAX)?;
assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));sourceยง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 = NonZero::<i8>::new(-1i8)?;
assert_eq!(n.leading_zeros(), 0);1.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 = NonZero::<i8>::new(0b0101000)?;
assert_eq!(n.trailing_zeros(), 3);sourcepub const fn count_ones(self) -> NonZero<u32>
๐ฌThis is a nightly-only experimental API. (non_zero_count_ones)
pub const fn count_ones(self) -> NonZero<u32>
non_zero_count_ones)Returns the number of ones in the binary representation of self.
ยงExamples
Basic usage:
#![feature(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)?);1.70.0 ยท sourcepub const MIN: NonZero<i8> = _
pub const MIN: NonZero<i8> = _
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!(NonZero::<i8>::MIN.get(), i8::MIN);1.70.0 ยท sourcepub const MAX: NonZero<i8> = _
pub const MAX: NonZero<i8> = _
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!(NonZero::<i8>::MAX.get(), i8::MAX);1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_abs(self) -> Option<NonZero<i8>>
pub const fn checked_abs(self) -> Option<NonZero<i8>>
Checked absolute value.
Checks for overflow and returns None if
self == NonZero::<i8>::MIN.
The result cannot be zero.
ยงExample
let pos = NonZero::new(1i8)?;
let neg = NonZero::new(-1i8)?;
let min = NonZero::new(i8::MIN)?;
assert_eq!(Some(pos), neg.checked_abs());
assert_eq!(None, min.checked_abs());1.64.0 (const: 1.64.0) ยท sourcepub const fn overflowing_abs(self) -> (NonZero<i8>, bool)
pub const fn overflowing_abs(self) -> (NonZero<i8>, bool)
Computes the absolute value of self,
with overflow information, see
i8::overflowing_abs.
ยงExample
let pos = NonZero::new(1i8)?;
let neg = NonZero::new(-1i8)?;
let min = NonZero::new(i8::MIN)?;
assert_eq!((pos, false), pos.overflowing_abs());
assert_eq!((pos, false), neg.overflowing_abs());
assert_eq!((min, true), min.overflowing_abs());1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_abs(self) -> NonZero<i8>
pub const fn saturating_abs(self) -> NonZero<i8>
Saturating absolute value, see
i8::saturating_abs.
ยงExample
let pos = NonZero::new(1i8)?;
let neg = NonZero::new(-1i8)?;
let min = NonZero::new(i8::MIN)?;
let min_plus = NonZero::new(i8::MIN + 1)?;
let max = NonZero::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());1.64.0 (const: 1.64.0) ยท sourcepub const fn wrapping_abs(self) -> NonZero<i8>
pub const fn wrapping_abs(self) -> NonZero<i8>
Wrapping absolute value, see
i8::wrapping_abs.
ยงExample
let pos = NonZero::new(1i8)?;
let neg = NonZero::new(-1i8)?;
let min = NonZero::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());1.64.0 (const: 1.64.0) ยท sourcepub const fn unsigned_abs(self) -> NonZero<u8>
pub const fn unsigned_abs(self) -> NonZero<u8>
Computes the absolute value of self without any wrapping or panicking.
ยงExample
let u_pos = NonZero::new(1u8)?;
let i_pos = NonZero::new(1i8)?;
let i_neg = NonZero::new(-1i8)?;
let i_min = NonZero::new(i8::MIN)?;
let u_max = NonZero::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());1.71.0 (const: 1.71.0) ยท sourcepub const fn is_positive(self) -> bool
pub const fn is_positive(self) -> bool
Returns true if self is positive and false if the
number is negative.
ยงExample
let pos_five = NonZero::new(5i8)?;
let neg_five = NonZero::new(-5i8)?;
assert!(pos_five.is_positive());
assert!(!neg_five.is_positive());1.71.0 (const: 1.71.0) ยท sourcepub const fn is_negative(self) -> bool
pub const fn is_negative(self) -> bool
Returns true if self is negative and false if the
number is positive.
ยงExample
let pos_five = NonZero::new(5i8)?;
let neg_five = NonZero::new(-5i8)?;
assert!(neg_five.is_negative());
assert!(!pos_five.is_negative());1.71.0 (const: 1.71.0) ยท sourcepub const fn checked_neg(self) -> Option<NonZero<i8>>
pub const fn checked_neg(self) -> Option<NonZero<i8>>
Checked negation. Computes -self,
returning None if self == NonZero::<i8>::MIN.
ยงExample
let pos_five = NonZero::new(5i8)?;
let neg_five = NonZero::new(-5i8)?;
let min = NonZero::new(i8::MIN)?;
assert_eq!(pos_five.checked_neg(), Some(neg_five));
assert_eq!(min.checked_neg(), None);1.71.0 (const: 1.71.0) ยท sourcepub const fn overflowing_neg(self) -> (NonZero<i8>, bool)
pub const fn overflowing_neg(self) -> (NonZero<i8>, 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 = NonZero::new(5i8)?;
let neg_five = NonZero::new(-5i8)?;
let min = NonZero::new(i8::MIN)?;
assert_eq!(pos_five.overflowing_neg(), (neg_five, false));
assert_eq!(min.overflowing_neg(), (min, true));1.71.0 (const: 1.71.0) ยท sourcepub const fn saturating_neg(self) -> NonZero<i8>
pub const fn saturating_neg(self) -> NonZero<i8>
Saturating negation. Computes -self,
returning NonZero::<i8>::MAX
if self == NonZero::<i8>::MIN
instead of overflowing.
ยงExample
let pos_five = NonZero::new(5i8)?;
let neg_five = NonZero::new(-5i8)?;
let min = NonZero::new(i8::MIN)?;
let min_plus_one = NonZero::new(i8::MIN + 1)?;
let max = NonZero::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);1.71.0 (const: 1.71.0) ยท sourcepub const fn wrapping_neg(self) -> NonZero<i8>
pub const fn wrapping_neg(self) -> NonZero<i8>
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 = NonZero::new(5i8)?;
let neg_five = NonZero::new(-5i8)?;
let min = NonZero::new(i8::MIN)?;
assert_eq!(pos_five.wrapping_neg(), neg_five);
assert_eq!(min.wrapping_neg(), min);1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_mul(self, other: NonZero<i8>) -> Option<NonZero<i8>>
pub const fn checked_mul(self, other: NonZero<i8>) -> Option<NonZero<i8>>
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 = NonZero::new(2i8)?;
let four = NonZero::new(4i8)?;
let max = NonZero::new(i8::MAX)?;
assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_mul(self, other: NonZero<i8>) -> NonZero<i8>
pub const fn saturating_mul(self, other: NonZero<i8>) -> NonZero<i8>
Multiplies two non-zero integers together.
Return NonZero::<i8>::MAX on overflow.
ยงExamples
let two = NonZero::new(2i8)?;
let four = NonZero::new(4i8)?;
let max = NonZero::new(i8::MAX)?;
assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));sourcepub const unsafe fn unchecked_mul(self, other: NonZero<i8>) -> NonZero<i8>
๐ฌThis is a nightly-only experimental API. (nonzero_ops)
pub const unsafe fn unchecked_mul(self, other: NonZero<i8>) -> NonZero<i8>
nonzero_ops)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 = NonZero::new(2i8)?;
let four = NonZero::new(4i8)?;
assert_eq!(four, unsafe { two.unchecked_mul(two) });1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_pow(self, other: u32) -> Option<NonZero<i8>>
pub const fn checked_pow(self, other: u32) -> Option<NonZero<i8>>
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 = NonZero::new(3i8)?;
let twenty_seven = NonZero::new(27i8)?;
let half_max = NonZero::new(i8::MAX / 2)?;
assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_pow(self, other: u32) -> NonZero<i8>
pub const fn saturating_pow(self, other: u32) -> NonZero<i8>
Raise non-zero value to an integer power.
Return NonZero::<i8>::MIN or NonZero::<i8>::MAX on overflow.
ยงExamples
let three = NonZero::new(3i8)?;
let twenty_seven = NonZero::new(27i8)?;
let max = NonZero::new(i8::MAX)?;
assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));sourceยง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 = NonZero::<i16>::new(-1i16)?;
assert_eq!(n.leading_zeros(), 0);1.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 = NonZero::<i16>::new(0b0101000)?;
assert_eq!(n.trailing_zeros(), 3);sourcepub const fn count_ones(self) -> NonZero<u32>
๐ฌThis is a nightly-only experimental API. (non_zero_count_ones)
pub const fn count_ones(self) -> NonZero<u32>
non_zero_count_ones)Returns the number of ones in the binary representation of self.
ยงExamples
Basic usage:
#![feature(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)?);1.70.0 ยท sourcepub const MIN: NonZero<i16> = _
pub const MIN: NonZero<i16> = _
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!(NonZero::<i16>::MIN.get(), i16::MIN);1.70.0 ยท sourcepub const MAX: NonZero<i16> = _
pub const MAX: NonZero<i16> = _
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!(NonZero::<i16>::MAX.get(), i16::MAX);1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_abs(self) -> Option<NonZero<i16>>
pub const fn checked_abs(self) -> Option<NonZero<i16>>
Checked absolute value.
Checks for overflow and returns None if
self == NonZero::<i16>::MIN.
The result cannot be zero.
ยงExample
let pos = NonZero::new(1i16)?;
let neg = NonZero::new(-1i16)?;
let min = NonZero::new(i16::MIN)?;
assert_eq!(Some(pos), neg.checked_abs());
assert_eq!(None, min.checked_abs());1.64.0 (const: 1.64.0) ยท sourcepub const fn overflowing_abs(self) -> (NonZero<i16>, bool)
pub const fn overflowing_abs(self) -> (NonZero<i16>, bool)
Computes the absolute value of self,
with overflow information, see
i16::overflowing_abs.
ยงExample
let pos = NonZero::new(1i16)?;
let neg = NonZero::new(-1i16)?;
let min = NonZero::new(i16::MIN)?;
assert_eq!((pos, false), pos.overflowing_abs());
assert_eq!((pos, false), neg.overflowing_abs());
assert_eq!((min, true), min.overflowing_abs());1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_abs(self) -> NonZero<i16>
pub const fn saturating_abs(self) -> NonZero<i16>
Saturating absolute value, see
i16::saturating_abs.
ยงExample
let pos = NonZero::new(1i16)?;
let neg = NonZero::new(-1i16)?;
let min = NonZero::new(i16::MIN)?;
let min_plus = NonZero::new(i16::MIN + 1)?;
let max = NonZero::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());1.64.0 (const: 1.64.0) ยท sourcepub const fn wrapping_abs(self) -> NonZero<i16>
pub const fn wrapping_abs(self) -> NonZero<i16>
Wrapping absolute value, see
i16::wrapping_abs.
ยงExample
let pos = NonZero::new(1i16)?;
let neg = NonZero::new(-1i16)?;
let min = NonZero::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());1.64.0 (const: 1.64.0) ยท sourcepub const fn unsigned_abs(self) -> NonZero<u16>
pub const fn unsigned_abs(self) -> NonZero<u16>
Computes the absolute value of self without any wrapping or panicking.
ยงExample
let u_pos = NonZero::new(1u16)?;
let i_pos = NonZero::new(1i16)?;
let i_neg = NonZero::new(-1i16)?;
let i_min = NonZero::new(i16::MIN)?;
let u_max = NonZero::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());1.71.0 (const: 1.71.0) ยท sourcepub const fn is_positive(self) -> bool
pub const fn is_positive(self) -> bool
Returns true if self is positive and false if the
number is negative.
ยงExample
let pos_five = NonZero::new(5i16)?;
let neg_five = NonZero::new(-5i16)?;
assert!(pos_five.is_positive());
assert!(!neg_five.is_positive());1.71.0 (const: 1.71.0) ยท sourcepub const fn is_negative(self) -> bool
pub const fn is_negative(self) -> bool
Returns true if self is negative and false if the
number is positive.
ยงExample
let pos_five = NonZero::new(5i16)?;
let neg_five = NonZero::new(-5i16)?;
assert!(neg_five.is_negative());
assert!(!pos_five.is_negative());1.71.0 (const: 1.71.0) ยท sourcepub const fn checked_neg(self) -> Option<NonZero<i16>>
pub const fn checked_neg(self) -> Option<NonZero<i16>>
Checked negation. Computes -self,
returning None if self == NonZero::<i16>::MIN.
ยงExample
let pos_five = NonZero::new(5i16)?;
let neg_five = NonZero::new(-5i16)?;
let min = NonZero::new(i16::MIN)?;
assert_eq!(pos_five.checked_neg(), Some(neg_five));
assert_eq!(min.checked_neg(), None);1.71.0 (const: 1.71.0) ยท sourcepub const fn overflowing_neg(self) -> (NonZero<i16>, bool)
pub const fn overflowing_neg(self) -> (NonZero<i16>, 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 = NonZero::new(5i16)?;
let neg_five = NonZero::new(-5i16)?;
let min = NonZero::new(i16::MIN)?;
assert_eq!(pos_five.overflowing_neg(), (neg_five, false));
assert_eq!(min.overflowing_neg(), (min, true));1.71.0 (const: 1.71.0) ยท sourcepub const fn saturating_neg(self) -> NonZero<i16>
pub const fn saturating_neg(self) -> NonZero<i16>
Saturating negation. Computes -self,
returning NonZero::<i16>::MAX
if self == NonZero::<i16>::MIN
instead of overflowing.
ยงExample
let pos_five = NonZero::new(5i16)?;
let neg_five = NonZero::new(-5i16)?;
let min = NonZero::new(i16::MIN)?;
let min_plus_one = NonZero::new(i16::MIN + 1)?;
let max = NonZero::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);1.71.0 (const: 1.71.0) ยท sourcepub const fn wrapping_neg(self) -> NonZero<i16>
pub const fn wrapping_neg(self) -> NonZero<i16>
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 = NonZero::new(5i16)?;
let neg_five = NonZero::new(-5i16)?;
let min = NonZero::new(i16::MIN)?;
assert_eq!(pos_five.wrapping_neg(), neg_five);
assert_eq!(min.wrapping_neg(), min);1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_mul(self, other: NonZero<i16>) -> Option<NonZero<i16>>
pub const fn checked_mul(self, other: NonZero<i16>) -> Option<NonZero<i16>>
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 = NonZero::new(2i16)?;
let four = NonZero::new(4i16)?;
let max = NonZero::new(i16::MAX)?;
assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_mul(self, other: NonZero<i16>) -> NonZero<i16>
pub const fn saturating_mul(self, other: NonZero<i16>) -> NonZero<i16>
Multiplies two non-zero integers together.
Return NonZero::<i16>::MAX on overflow.
ยงExamples
let two = NonZero::new(2i16)?;
let four = NonZero::new(4i16)?;
let max = NonZero::new(i16::MAX)?;
assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));sourcepub const unsafe fn unchecked_mul(self, other: NonZero<i16>) -> NonZero<i16>
๐ฌThis is a nightly-only experimental API. (nonzero_ops)
pub const unsafe fn unchecked_mul(self, other: NonZero<i16>) -> NonZero<i16>
nonzero_ops)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 = NonZero::new(2i16)?;
let four = NonZero::new(4i16)?;
assert_eq!(four, unsafe { two.unchecked_mul(two) });1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_pow(self, other: u32) -> Option<NonZero<i16>>
pub const fn checked_pow(self, other: u32) -> Option<NonZero<i16>>
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 = NonZero::new(3i16)?;
let twenty_seven = NonZero::new(27i16)?;
let half_max = NonZero::new(i16::MAX / 2)?;
assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_pow(self, other: u32) -> NonZero<i16>
pub const fn saturating_pow(self, other: u32) -> NonZero<i16>
Raise non-zero value to an integer power.
Return NonZero::<i16>::MIN or NonZero::<i16>::MAX on overflow.
ยงExamples
let three = NonZero::new(3i16)?;
let twenty_seven = NonZero::new(27i16)?;
let max = NonZero::new(i16::MAX)?;
assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));sourceยง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 = NonZero::<i32>::new(-1i32)?;
assert_eq!(n.leading_zeros(), 0);1.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 = NonZero::<i32>::new(0b0101000)?;
assert_eq!(n.trailing_zeros(), 3);sourcepub const fn count_ones(self) -> NonZero<u32>
๐ฌThis is a nightly-only experimental API. (non_zero_count_ones)
pub const fn count_ones(self) -> NonZero<u32>
non_zero_count_ones)Returns the number of ones in the binary representation of self.
ยงExamples
Basic usage:
#![feature(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)?);1.70.0 ยท sourcepub const MIN: NonZero<i32> = _
pub const MIN: NonZero<i32> = _
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!(NonZero::<i32>::MIN.get(), i32::MIN);1.70.0 ยท sourcepub const MAX: NonZero<i32> = _
pub const MAX: NonZero<i32> = _
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!(NonZero::<i32>::MAX.get(), i32::MAX);1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_abs(self) -> Option<NonZero<i32>>
pub const fn checked_abs(self) -> Option<NonZero<i32>>
Checked absolute value.
Checks for overflow and returns None if
self == NonZero::<i32>::MIN.
The result cannot be zero.
ยงExample
let pos = NonZero::new(1i32)?;
let neg = NonZero::new(-1i32)?;
let min = NonZero::new(i32::MIN)?;
assert_eq!(Some(pos), neg.checked_abs());
assert_eq!(None, min.checked_abs());1.64.0 (const: 1.64.0) ยท sourcepub const fn overflowing_abs(self) -> (NonZero<i32>, bool)
pub const fn overflowing_abs(self) -> (NonZero<i32>, bool)
Computes the absolute value of self,
with overflow information, see
i32::overflowing_abs.
ยงExample
let pos = NonZero::new(1i32)?;
let neg = NonZero::new(-1i32)?;
let min = NonZero::new(i32::MIN)?;
assert_eq!((pos, false), pos.overflowing_abs());
assert_eq!((pos, false), neg.overflowing_abs());
assert_eq!((min, true), min.overflowing_abs());1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_abs(self) -> NonZero<i32>
pub const fn saturating_abs(self) -> NonZero<i32>
Saturating absolute value, see
i32::saturating_abs.
ยงExample
let pos = NonZero::new(1i32)?;
let neg = NonZero::new(-1i32)?;
let min = NonZero::new(i32::MIN)?;
let min_plus = NonZero::new(i32::MIN + 1)?;
let max = NonZero::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());1.64.0 (const: 1.64.0) ยท sourcepub const fn wrapping_abs(self) -> NonZero<i32>
pub const fn wrapping_abs(self) -> NonZero<i32>
Wrapping absolute value, see
i32::wrapping_abs.
ยงExample
let pos = NonZero::new(1i32)?;
let neg = NonZero::new(-1i32)?;
let min = NonZero::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());1.64.0 (const: 1.64.0) ยท sourcepub const fn unsigned_abs(self) -> NonZero<u32>
pub const fn unsigned_abs(self) -> NonZero<u32>
Computes the absolute value of self without any wrapping or panicking.
ยงExample
let u_pos = NonZero::new(1u32)?;
let i_pos = NonZero::new(1i32)?;
let i_neg = NonZero::new(-1i32)?;
let i_min = NonZero::new(i32::MIN)?;
let u_max = NonZero::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());1.71.0 (const: 1.71.0) ยท sourcepub const fn is_positive(self) -> bool
pub const fn is_positive(self) -> bool
Returns true if self is positive and false if the
number is negative.
ยงExample
let pos_five = NonZero::new(5i32)?;
let neg_five = NonZero::new(-5i32)?;
assert!(pos_five.is_positive());
assert!(!neg_five.is_positive());1.71.0 (const: 1.71.0) ยท sourcepub const fn is_negative(self) -> bool
pub const fn is_negative(self) -> bool
Returns true if self is negative and false if the
number is positive.
ยงExample
let pos_five = NonZero::new(5i32)?;
let neg_five = NonZero::new(-5i32)?;
assert!(neg_five.is_negative());
assert!(!pos_five.is_negative());1.71.0 (const: 1.71.0) ยท sourcepub const fn checked_neg(self) -> Option<NonZero<i32>>
pub const fn checked_neg(self) -> Option<NonZero<i32>>
Checked negation. Computes -self,
returning None if self == NonZero::<i32>::MIN.
ยงExample
let pos_five = NonZero::new(5i32)?;
let neg_five = NonZero::new(-5i32)?;
let min = NonZero::new(i32::MIN)?;
assert_eq!(pos_five.checked_neg(), Some(neg_five));
assert_eq!(min.checked_neg(), None);1.71.0 (const: 1.71.0) ยท sourcepub const fn overflowing_neg(self) -> (NonZero<i32>, bool)
pub const fn overflowing_neg(self) -> (NonZero<i32>, 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 = NonZero::new(5i32)?;
let neg_five = NonZero::new(-5i32)?;
let min = NonZero::new(i32::MIN)?;
assert_eq!(pos_five.overflowing_neg(), (neg_five, false));
assert_eq!(min.overflowing_neg(), (min, true));1.71.0 (const: 1.71.0) ยท sourcepub const fn saturating_neg(self) -> NonZero<i32>
pub const fn saturating_neg(self) -> NonZero<i32>
Saturating negation. Computes -self,
returning NonZero::<i32>::MAX
if self == NonZero::<i32>::MIN
instead of overflowing.
ยงExample
let pos_five = NonZero::new(5i32)?;
let neg_five = NonZero::new(-5i32)?;
let min = NonZero::new(i32::MIN)?;
let min_plus_one = NonZero::new(i32::MIN + 1)?;
let max = NonZero::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);1.71.0 (const: 1.71.0) ยท sourcepub const fn wrapping_neg(self) -> NonZero<i32>
pub const fn wrapping_neg(self) -> NonZero<i32>
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 = NonZero::new(5i32)?;
let neg_five = NonZero::new(-5i32)?;
let min = NonZero::new(i32::MIN)?;
assert_eq!(pos_five.wrapping_neg(), neg_five);
assert_eq!(min.wrapping_neg(), min);1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_mul(self, other: NonZero<i32>) -> Option<NonZero<i32>>
pub const fn checked_mul(self, other: NonZero<i32>) -> Option<NonZero<i32>>
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 = NonZero::new(2i32)?;
let four = NonZero::new(4i32)?;
let max = NonZero::new(i32::MAX)?;
assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_mul(self, other: NonZero<i32>) -> NonZero<i32>
pub const fn saturating_mul(self, other: NonZero<i32>) -> NonZero<i32>
Multiplies two non-zero integers together.
Return NonZero::<i32>::MAX on overflow.
ยงExamples
let two = NonZero::new(2i32)?;
let four = NonZero::new(4i32)?;
let max = NonZero::new(i32::MAX)?;
assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));sourcepub const unsafe fn unchecked_mul(self, other: NonZero<i32>) -> NonZero<i32>
๐ฌThis is a nightly-only experimental API. (nonzero_ops)
pub const unsafe fn unchecked_mul(self, other: NonZero<i32>) -> NonZero<i32>
nonzero_ops)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 = NonZero::new(2i32)?;
let four = NonZero::new(4i32)?;
assert_eq!(four, unsafe { two.unchecked_mul(two) });1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_pow(self, other: u32) -> Option<NonZero<i32>>
pub const fn checked_pow(self, other: u32) -> Option<NonZero<i32>>
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 = NonZero::new(3i32)?;
let twenty_seven = NonZero::new(27i32)?;
let half_max = NonZero::new(i32::MAX / 2)?;
assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_pow(self, other: u32) -> NonZero<i32>
pub const fn saturating_pow(self, other: u32) -> NonZero<i32>
Raise non-zero value to an integer power.
Return NonZero::<i32>::MIN or NonZero::<i32>::MAX on overflow.
ยงExamples
let three = NonZero::new(3i32)?;
let twenty_seven = NonZero::new(27i32)?;
let max = NonZero::new(i32::MAX)?;
assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));sourceยง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 = NonZero::<i64>::new(-1i64)?;
assert_eq!(n.leading_zeros(), 0);1.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 = NonZero::<i64>::new(0b0101000)?;
assert_eq!(n.trailing_zeros(), 3);sourcepub const fn count_ones(self) -> NonZero<u32>
๐ฌThis is a nightly-only experimental API. (non_zero_count_ones)
pub const fn count_ones(self) -> NonZero<u32>
non_zero_count_ones)Returns the number of ones in the binary representation of self.
ยงExamples
Basic usage:
#![feature(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)?);1.70.0 ยท sourcepub const MIN: NonZero<i64> = _
pub const MIN: NonZero<i64> = _
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!(NonZero::<i64>::MIN.get(), i64::MIN);1.70.0 ยท sourcepub const MAX: NonZero<i64> = _
pub const MAX: NonZero<i64> = _
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!(NonZero::<i64>::MAX.get(), i64::MAX);1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_abs(self) -> Option<NonZero<i64>>
pub const fn checked_abs(self) -> Option<NonZero<i64>>
Checked absolute value.
Checks for overflow and returns None if
self == NonZero::<i64>::MIN.
The result cannot be zero.
ยงExample
let pos = NonZero::new(1i64)?;
let neg = NonZero::new(-1i64)?;
let min = NonZero::new(i64::MIN)?;
assert_eq!(Some(pos), neg.checked_abs());
assert_eq!(None, min.checked_abs());1.64.0 (const: 1.64.0) ยท sourcepub const fn overflowing_abs(self) -> (NonZero<i64>, bool)
pub const fn overflowing_abs(self) -> (NonZero<i64>, bool)
Computes the absolute value of self,
with overflow information, see
i64::overflowing_abs.
ยงExample
let pos = NonZero::new(1i64)?;
let neg = NonZero::new(-1i64)?;
let min = NonZero::new(i64::MIN)?;
assert_eq!((pos, false), pos.overflowing_abs());
assert_eq!((pos, false), neg.overflowing_abs());
assert_eq!((min, true), min.overflowing_abs());1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_abs(self) -> NonZero<i64>
pub const fn saturating_abs(self) -> NonZero<i64>
Saturating absolute value, see
i64::saturating_abs.
ยงExample
let pos = NonZero::new(1i64)?;
let neg = NonZero::new(-1i64)?;
let min = NonZero::new(i64::MIN)?;
let min_plus = NonZero::new(i64::MIN + 1)?;
let max = NonZero::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());1.64.0 (const: 1.64.0) ยท sourcepub const fn wrapping_abs(self) -> NonZero<i64>
pub const fn wrapping_abs(self) -> NonZero<i64>
Wrapping absolute value, see
i64::wrapping_abs.
ยงExample
let pos = NonZero::new(1i64)?;
let neg = NonZero::new(-1i64)?;
let min = NonZero::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());1.64.0 (const: 1.64.0) ยท sourcepub const fn unsigned_abs(self) -> NonZero<u64>
pub const fn unsigned_abs(self) -> NonZero<u64>
Computes the absolute value of self without any wrapping or panicking.
ยงExample
let u_pos = NonZero::new(1u64)?;
let i_pos = NonZero::new(1i64)?;
let i_neg = NonZero::new(-1i64)?;
let i_min = NonZero::new(i64::MIN)?;
let u_max = NonZero::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());1.71.0 (const: 1.71.0) ยท sourcepub const fn is_positive(self) -> bool
pub const fn is_positive(self) -> bool
Returns true if self is positive and false if the
number is negative.
ยงExample
let pos_five = NonZero::new(5i64)?;
let neg_five = NonZero::new(-5i64)?;
assert!(pos_five.is_positive());
assert!(!neg_five.is_positive());1.71.0 (const: 1.71.0) ยท sourcepub const fn is_negative(self) -> bool
pub const fn is_negative(self) -> bool
Returns true if self is negative and false if the
number is positive.
ยงExample
let pos_five = NonZero::new(5i64)?;
let neg_five = NonZero::new(-5i64)?;
assert!(neg_five.is_negative());
assert!(!pos_five.is_negative());1.71.0 (const: 1.71.0) ยท sourcepub const fn checked_neg(self) -> Option<NonZero<i64>>
pub const fn checked_neg(self) -> Option<NonZero<i64>>
Checked negation. Computes -self,
returning None if self == NonZero::<i64>::MIN.
ยงExample
let pos_five = NonZero::new(5i64)?;
let neg_five = NonZero::new(-5i64)?;
let min = NonZero::new(i64::MIN)?;
assert_eq!(pos_five.checked_neg(), Some(neg_five));
assert_eq!(min.checked_neg(), None);1.71.0 (const: 1.71.0) ยท sourcepub const fn overflowing_neg(self) -> (NonZero<i64>, bool)
pub const fn overflowing_neg(self) -> (NonZero<i64>, 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 = NonZero::new(5i64)?;
let neg_five = NonZero::new(-5i64)?;
let min = NonZero::new(i64::MIN)?;
assert_eq!(pos_five.overflowing_neg(), (neg_five, false));
assert_eq!(min.overflowing_neg(), (min, true));1.71.0 (const: 1.71.0) ยท sourcepub const fn saturating_neg(self) -> NonZero<i64>
pub const fn saturating_neg(self) -> NonZero<i64>
Saturating negation. Computes -self,
returning NonZero::<i64>::MAX
if self == NonZero::<i64>::MIN
instead of overflowing.
ยงExample
let pos_five = NonZero::new(5i64)?;
let neg_five = NonZero::new(-5i64)?;
let min = NonZero::new(i64::MIN)?;
let min_plus_one = NonZero::new(i64::MIN + 1)?;
let max = NonZero::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);1.71.0 (const: 1.71.0) ยท sourcepub const fn wrapping_neg(self) -> NonZero<i64>
pub const fn wrapping_neg(self) -> NonZero<i64>
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 = NonZero::new(5i64)?;
let neg_five = NonZero::new(-5i64)?;
let min = NonZero::new(i64::MIN)?;
assert_eq!(pos_five.wrapping_neg(), neg_five);
assert_eq!(min.wrapping_neg(), min);1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_mul(self, other: NonZero<i64>) -> Option<NonZero<i64>>
pub const fn checked_mul(self, other: NonZero<i64>) -> Option<NonZero<i64>>
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 = NonZero::new(2i64)?;
let four = NonZero::new(4i64)?;
let max = NonZero::new(i64::MAX)?;
assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_mul(self, other: NonZero<i64>) -> NonZero<i64>
pub const fn saturating_mul(self, other: NonZero<i64>) -> NonZero<i64>
Multiplies two non-zero integers together.
Return NonZero::<i64>::MAX on overflow.
ยงExamples
let two = NonZero::new(2i64)?;
let four = NonZero::new(4i64)?;
let max = NonZero::new(i64::MAX)?;
assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));sourcepub const unsafe fn unchecked_mul(self, other: NonZero<i64>) -> NonZero<i64>
๐ฌThis is a nightly-only experimental API. (nonzero_ops)
pub const unsafe fn unchecked_mul(self, other: NonZero<i64>) -> NonZero<i64>
nonzero_ops)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 = NonZero::new(2i64)?;
let four = NonZero::new(4i64)?;
assert_eq!(four, unsafe { two.unchecked_mul(two) });1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_pow(self, other: u32) -> Option<NonZero<i64>>
pub const fn checked_pow(self, other: u32) -> Option<NonZero<i64>>
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 = NonZero::new(3i64)?;
let twenty_seven = NonZero::new(27i64)?;
let half_max = NonZero::new(i64::MAX / 2)?;
assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_pow(self, other: u32) -> NonZero<i64>
pub const fn saturating_pow(self, other: u32) -> NonZero<i64>
Raise non-zero value to an integer power.
Return NonZero::<i64>::MIN or NonZero::<i64>::MAX on overflow.
ยงExamples
let three = NonZero::new(3i64)?;
let twenty_seven = NonZero::new(27i64)?;
let max = NonZero::new(i64::MAX)?;
assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));sourceยง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!(NonZero::<i128>::BITS, i128::BITS);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 = NonZero::<i128>::new(-1i128)?;
assert_eq!(n.leading_zeros(), 0);1.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 = NonZero::<i128>::new(0b0101000)?;
assert_eq!(n.trailing_zeros(), 3);sourcepub const fn count_ones(self) -> NonZero<u32>
๐ฌThis is a nightly-only experimental API. (non_zero_count_ones)
pub const fn count_ones(self) -> NonZero<u32>
non_zero_count_ones)Returns the number of ones in the binary representation of self.
ยงExamples
Basic usage:
#![feature(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)?);1.70.0 ยท sourcepub const MIN: NonZero<i128> = _
pub const MIN: NonZero<i128> = _
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!(NonZero::<i128>::MIN.get(), i128::MIN);1.70.0 ยท sourcepub const MAX: NonZero<i128> = _
pub const MAX: NonZero<i128> = _
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!(NonZero::<i128>::MAX.get(), i128::MAX);1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_abs(self) -> Option<NonZero<i128>>
pub const fn checked_abs(self) -> Option<NonZero<i128>>
Checked absolute value.
Checks for overflow and returns None if
self == NonZero::<i128>::MIN.
The result cannot be zero.
ยงExample
let pos = NonZero::new(1i128)?;
let neg = NonZero::new(-1i128)?;
let min = NonZero::new(i128::MIN)?;
assert_eq!(Some(pos), neg.checked_abs());
assert_eq!(None, min.checked_abs());1.64.0 (const: 1.64.0) ยท sourcepub const fn overflowing_abs(self) -> (NonZero<i128>, bool)
pub const fn overflowing_abs(self) -> (NonZero<i128>, bool)
Computes the absolute value of self,
with overflow information, see
i128::overflowing_abs.
ยงExample
let pos = NonZero::new(1i128)?;
let neg = NonZero::new(-1i128)?;
let min = NonZero::new(i128::MIN)?;
assert_eq!((pos, false), pos.overflowing_abs());
assert_eq!((pos, false), neg.overflowing_abs());
assert_eq!((min, true), min.overflowing_abs());1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_abs(self) -> NonZero<i128>
pub const fn saturating_abs(self) -> NonZero<i128>
Saturating absolute value, see
i128::saturating_abs.
ยงExample
let pos = NonZero::new(1i128)?;
let neg = NonZero::new(-1i128)?;
let min = NonZero::new(i128::MIN)?;
let min_plus = NonZero::new(i128::MIN + 1)?;
let max = NonZero::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());1.64.0 (const: 1.64.0) ยท sourcepub const fn wrapping_abs(self) -> NonZero<i128>
pub const fn wrapping_abs(self) -> NonZero<i128>
Wrapping absolute value, see
i128::wrapping_abs.
ยงExample
let pos = NonZero::new(1i128)?;
let neg = NonZero::new(-1i128)?;
let min = NonZero::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());1.64.0 (const: 1.64.0) ยท sourcepub const fn unsigned_abs(self) -> NonZero<u128>
pub const fn unsigned_abs(self) -> NonZero<u128>
Computes the absolute value of self without any wrapping or panicking.
ยงExample
let u_pos = NonZero::new(1u128)?;
let i_pos = NonZero::new(1i128)?;
let i_neg = NonZero::new(-1i128)?;
let i_min = NonZero::new(i128::MIN)?;
let u_max = NonZero::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());1.71.0 (const: 1.71.0) ยท sourcepub const fn is_positive(self) -> bool
pub const fn is_positive(self) -> bool
Returns true if self is positive and false if the
number is negative.
ยงExample
let pos_five = NonZero::new(5i128)?;
let neg_five = NonZero::new(-5i128)?;
assert!(pos_five.is_positive());
assert!(!neg_five.is_positive());1.71.0 (const: 1.71.0) ยท sourcepub const fn is_negative(self) -> bool
pub const fn is_negative(self) -> bool
Returns true if self is negative and false if the
number is positive.
ยงExample
let pos_five = NonZero::new(5i128)?;
let neg_five = NonZero::new(-5i128)?;
assert!(neg_five.is_negative());
assert!(!pos_five.is_negative());1.71.0 (const: 1.71.0) ยท sourcepub const fn checked_neg(self) -> Option<NonZero<i128>>
pub const fn checked_neg(self) -> Option<NonZero<i128>>
Checked negation. Computes -self,
returning None if self == NonZero::<i128>::MIN.
ยงExample
let pos_five = NonZero::new(5i128)?;
let neg_five = NonZero::new(-5i128)?;
let min = NonZero::new(i128::MIN)?;
assert_eq!(pos_five.checked_neg(), Some(neg_five));
assert_eq!(min.checked_neg(), None);1.71.0 (const: 1.71.0) ยท sourcepub const fn overflowing_neg(self) -> (NonZero<i128>, bool)
pub const fn overflowing_neg(self) -> (NonZero<i128>, 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 = NonZero::new(5i128)?;
let neg_five = NonZero::new(-5i128)?;
let min = NonZero::new(i128::MIN)?;
assert_eq!(pos_five.overflowing_neg(), (neg_five, false));
assert_eq!(min.overflowing_neg(), (min, true));1.71.0 (const: 1.71.0) ยท sourcepub const fn saturating_neg(self) -> NonZero<i128>
pub const fn saturating_neg(self) -> NonZero<i128>
Saturating negation. Computes -self,
returning NonZero::<i128>::MAX
if self == NonZero::<i128>::MIN
instead of overflowing.
ยงExample
let pos_five = NonZero::new(5i128)?;
let neg_five = NonZero::new(-5i128)?;
let min = NonZero::new(i128::MIN)?;
let min_plus_one = NonZero::new(i128::MIN + 1)?;
let max = NonZero::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);1.71.0 (const: 1.71.0) ยท sourcepub const fn wrapping_neg(self) -> NonZero<i128>
pub const fn wrapping_neg(self) -> NonZero<i128>
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 = NonZero::new(5i128)?;
let neg_five = NonZero::new(-5i128)?;
let min = NonZero::new(i128::MIN)?;
assert_eq!(pos_five.wrapping_neg(), neg_five);
assert_eq!(min.wrapping_neg(), min);1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_mul(self, other: NonZero<i128>) -> Option<NonZero<i128>>
pub const fn checked_mul(self, other: NonZero<i128>) -> Option<NonZero<i128>>
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 = NonZero::new(2i128)?;
let four = NonZero::new(4i128)?;
let max = NonZero::new(i128::MAX)?;
assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_mul(self, other: NonZero<i128>) -> NonZero<i128>
pub const fn saturating_mul(self, other: NonZero<i128>) -> NonZero<i128>
Multiplies two non-zero integers together.
Return NonZero::<i128>::MAX on overflow.
ยงExamples
let two = NonZero::new(2i128)?;
let four = NonZero::new(4i128)?;
let max = NonZero::new(i128::MAX)?;
assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));sourcepub const unsafe fn unchecked_mul(self, other: NonZero<i128>) -> NonZero<i128>
๐ฌThis is a nightly-only experimental API. (nonzero_ops)
pub const unsafe fn unchecked_mul(self, other: NonZero<i128>) -> NonZero<i128>
nonzero_ops)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 = NonZero::new(2i128)?;
let four = NonZero::new(4i128)?;
assert_eq!(four, unsafe { two.unchecked_mul(two) });1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_pow(self, other: u32) -> Option<NonZero<i128>>
pub const fn checked_pow(self, other: u32) -> Option<NonZero<i128>>
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 = NonZero::new(3i128)?;
let twenty_seven = NonZero::new(27i128)?;
let half_max = NonZero::new(i128::MAX / 2)?;
assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_pow(self, other: u32) -> NonZero<i128>
pub const fn saturating_pow(self, other: u32) -> NonZero<i128>
Raise non-zero value to an integer power.
Return NonZero::<i128>::MIN or NonZero::<i128>::MAX on overflow.
ยงExamples
let three = NonZero::new(3i128)?;
let twenty_seven = NonZero::new(27i128)?;
let max = NonZero::new(i128::MAX)?;
assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));sourceยงimpl NonZero<isize>
impl NonZero<isize>
1.67.0 ยท sourcepub const BITS: u32 = 32u32
pub const BITS: u32 = 32u32
The size of this non-zero integer type in bits.
This value is equal to isize::BITS.
ยงExamples
assert_eq!(NonZero::<isize>::BITS, isize::BITS);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 = NonZero::<isize>::new(-1isize)?;
assert_eq!(n.leading_zeros(), 0);1.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 = NonZero::<isize>::new(0b0101000)?;
assert_eq!(n.trailing_zeros(), 3);sourcepub const fn count_ones(self) -> NonZero<u32>
๐ฌThis is a nightly-only experimental API. (non_zero_count_ones)
pub const fn count_ones(self) -> NonZero<u32>
non_zero_count_ones)Returns the number of ones in the binary representation of self.
ยงExamples
Basic usage:
#![feature(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)?);1.70.0 ยท sourcepub const MIN: NonZero<isize> = _
pub const MIN: NonZero<isize> = _
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!(NonZero::<isize>::MIN.get(), isize::MIN);1.70.0 ยท sourcepub const MAX: NonZero<isize> = _
pub const MAX: NonZero<isize> = _
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!(NonZero::<isize>::MAX.get(), isize::MAX);1.64.0 (const: 1.64.0) ยท sourcepub const fn abs(self) -> NonZero<isize>
pub const fn abs(self) -> NonZero<isize>
Computes the absolute value of self.
See isize::abs
for documentation on overflow behaviour.
ยงExample
let pos = NonZero::new(1isize)?;
let neg = NonZero::new(-1isize)?;
assert_eq!(pos, pos.abs());
assert_eq!(pos, neg.abs());1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_abs(self) -> Option<NonZero<isize>>
pub const fn checked_abs(self) -> Option<NonZero<isize>>
Checked absolute value.
Checks for overflow and returns None if
self == NonZero::<isize>::MIN.
The result cannot be zero.
ยงExample
let pos = NonZero::new(1isize)?;
let neg = NonZero::new(-1isize)?;
let min = NonZero::new(isize::MIN)?;
assert_eq!(Some(pos), neg.checked_abs());
assert_eq!(None, min.checked_abs());1.64.0 (const: 1.64.0) ยท sourcepub const fn overflowing_abs(self) -> (NonZero<isize>, bool)
pub const fn overflowing_abs(self) -> (NonZero<isize>, bool)
Computes the absolute value of self,
with overflow information, see
isize::overflowing_abs.
ยงExample
let pos = NonZero::new(1isize)?;
let neg = NonZero::new(-1isize)?;
let min = NonZero::new(isize::MIN)?;
assert_eq!((pos, false), pos.overflowing_abs());
assert_eq!((pos, false), neg.overflowing_abs());
assert_eq!((min, true), min.overflowing_abs());1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_abs(self) -> NonZero<isize>
pub const fn saturating_abs(self) -> NonZero<isize>
Saturating absolute value, see
isize::saturating_abs.
ยงExample
let pos = NonZero::new(1isize)?;
let neg = NonZero::new(-1isize)?;
let min = NonZero::new(isize::MIN)?;
let min_plus = NonZero::new(isize::MIN + 1)?;
let max = NonZero::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());1.64.0 (const: 1.64.0) ยท sourcepub const fn wrapping_abs(self) -> NonZero<isize>
pub const fn wrapping_abs(self) -> NonZero<isize>
Wrapping absolute value, see
isize::wrapping_abs.
ยงExample
let pos = NonZero::new(1isize)?;
let neg = NonZero::new(-1isize)?;
let min = NonZero::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());1.64.0 (const: 1.64.0) ยท sourcepub const fn unsigned_abs(self) -> NonZero<usize>
pub const fn unsigned_abs(self) -> NonZero<usize>
Computes the absolute value of self without any wrapping or panicking.
ยงExample
let u_pos = NonZero::new(1usize)?;
let i_pos = NonZero::new(1isize)?;
let i_neg = NonZero::new(-1isize)?;
let i_min = NonZero::new(isize::MIN)?;
let u_max = NonZero::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());1.71.0 (const: 1.71.0) ยท sourcepub const fn is_positive(self) -> bool
pub const fn is_positive(self) -> bool
Returns true if self is positive and false if the
number is negative.
ยงExample
let pos_five = NonZero::new(5isize)?;
let neg_five = NonZero::new(-5isize)?;
assert!(pos_five.is_positive());
assert!(!neg_five.is_positive());1.71.0 (const: 1.71.0) ยท sourcepub const fn is_negative(self) -> bool
pub const fn is_negative(self) -> bool
Returns true if self is negative and false if the
number is positive.
ยงExample
let pos_five = NonZero::new(5isize)?;
let neg_five = NonZero::new(-5isize)?;
assert!(neg_five.is_negative());
assert!(!pos_five.is_negative());1.71.0 (const: 1.71.0) ยท sourcepub const fn checked_neg(self) -> Option<NonZero<isize>>
pub const fn checked_neg(self) -> Option<NonZero<isize>>
Checked negation. Computes -self,
returning None if self == NonZero::<isize>::MIN.
ยงExample
let pos_five = NonZero::new(5isize)?;
let neg_five = NonZero::new(-5isize)?;
let min = NonZero::new(isize::MIN)?;
assert_eq!(pos_five.checked_neg(), Some(neg_five));
assert_eq!(min.checked_neg(), None);1.71.0 (const: 1.71.0) ยท sourcepub const fn overflowing_neg(self) -> (NonZero<isize>, bool)
pub const fn overflowing_neg(self) -> (NonZero<isize>, 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 = NonZero::new(5isize)?;
let neg_five = NonZero::new(-5isize)?;
let min = NonZero::new(isize::MIN)?;
assert_eq!(pos_five.overflowing_neg(), (neg_five, false));
assert_eq!(min.overflowing_neg(), (min, true));1.71.0 (const: 1.71.0) ยท sourcepub const fn saturating_neg(self) -> NonZero<isize>
pub const fn saturating_neg(self) -> NonZero<isize>
Saturating negation. Computes -self,
returning NonZero::<isize>::MAX
if self == NonZero::<isize>::MIN
instead of overflowing.
ยงExample
let pos_five = NonZero::new(5isize)?;
let neg_five = NonZero::new(-5isize)?;
let min = NonZero::new(isize::MIN)?;
let min_plus_one = NonZero::new(isize::MIN + 1)?;
let max = NonZero::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);1.71.0 (const: 1.71.0) ยท sourcepub const fn wrapping_neg(self) -> NonZero<isize>
pub const fn wrapping_neg(self) -> NonZero<isize>
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 = NonZero::new(5isize)?;
let neg_five = NonZero::new(-5isize)?;
let min = NonZero::new(isize::MIN)?;
assert_eq!(pos_five.wrapping_neg(), neg_five);
assert_eq!(min.wrapping_neg(), min);1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_mul(self, other: NonZero<isize>) -> Option<NonZero<isize>>
pub const fn checked_mul(self, other: NonZero<isize>) -> Option<NonZero<isize>>
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 = NonZero::new(2isize)?;
let four = NonZero::new(4isize)?;
let max = NonZero::new(isize::MAX)?;
assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_mul(self, other: NonZero<isize>) -> NonZero<isize>
pub const fn saturating_mul(self, other: NonZero<isize>) -> NonZero<isize>
Multiplies two non-zero integers together.
Return NonZero::<isize>::MAX on overflow.
ยงExamples
let two = NonZero::new(2isize)?;
let four = NonZero::new(4isize)?;
let max = NonZero::new(isize::MAX)?;
assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));sourcepub const unsafe fn unchecked_mul(self, other: NonZero<isize>) -> NonZero<isize>
๐ฌThis is a nightly-only experimental API. (nonzero_ops)
pub const unsafe fn unchecked_mul(self, other: NonZero<isize>) -> NonZero<isize>
nonzero_ops)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 = NonZero::new(2isize)?;
let four = NonZero::new(4isize)?;
assert_eq!(four, unsafe { two.unchecked_mul(two) });1.64.0 (const: 1.64.0) ยท sourcepub const fn checked_pow(self, other: u32) -> Option<NonZero<isize>>
pub const fn checked_pow(self, other: u32) -> Option<NonZero<isize>>
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 = NonZero::new(3isize)?;
let twenty_seven = NonZero::new(27isize)?;
let half_max = NonZero::new(isize::MAX / 2)?;
assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));1.64.0 (const: 1.64.0) ยท sourcepub const fn saturating_pow(self, other: u32) -> NonZero<isize>
pub const fn saturating_pow(self, other: u32) -> NonZero<isize>
Raise non-zero value to an integer power.
Return NonZero::<isize>::MIN or NonZero::<isize>::MAX on overflow.
ยงExamples
let three = NonZero::new(3isize)?;
let twenty_seven = NonZero::new(27isize)?;
let max = NonZero::new(isize::MAX)?;
assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));Trait Implementationsยง
sourceยงimpl AsBytes for NonZero<i128>
impl AsBytes for NonZero<i128>
sourceยงimpl AsBytes for NonZero<i16>
impl AsBytes for NonZero<i16>
sourceยงimpl AsBytes for NonZero<i32>
impl AsBytes for NonZero<i32>
sourceยงimpl AsBytes for NonZero<i64>
impl AsBytes for NonZero<i64>
sourceยงimpl AsBytes for NonZero<i8>
impl AsBytes for NonZero<i8>
sourceยงimpl AsBytes for NonZero<isize>
impl AsBytes for NonZero<isize>
sourceยงimpl AsBytes for NonZero<u128>
impl AsBytes for NonZero<u128>
sourceยงimpl AsBytes for NonZero<u16>
impl AsBytes for NonZero<u16>
sourceยงimpl AsBytes for NonZero<u32>
impl AsBytes for NonZero<u32>
sourceยงimpl AsBytes for NonZero<u64>
impl AsBytes for NonZero<u64>
sourceยงimpl AsBytes for NonZero<u8>
impl AsBytes for NonZero<u8>
sourceยงimpl AsBytes for NonZero<usize>
impl AsBytes for NonZero<usize>
1.45.0 ยท sourceยงimpl<T> BitOrAssign<T> for NonZero<T>
impl<T> BitOrAssign<T> for NonZero<T>
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>
impl<T> BitOrAssign for NonZero<T>
sourceยงfn bitor_assign(&mut self, rhs: NonZero<T>)
fn bitor_assign(&mut self, rhs: NonZero<T>)
|= operation. Read moresourceยงimpl<'de> BorrowDecode<'de> for NonZero<i128>
impl<'de> BorrowDecode<'de> for NonZero<i128>
sourceยงfn borrow_decode<D>(decoder: &mut D) -> Result<NonZero<i128>, DecodeError>where
D: BorrowDecoder<'de>,
fn borrow_decode<D>(decoder: &mut D) -> Result<NonZero<i128>, DecodeError>where
D: BorrowDecoder<'de>,
sourceยงimpl<'de> BorrowDecode<'de> for NonZero<i16>
impl<'de> BorrowDecode<'de> for NonZero<i16>
sourceยงfn borrow_decode<D>(decoder: &mut D) -> Result<NonZero<i16>, DecodeError>where
D: BorrowDecoder<'de>,
fn borrow_decode<D>(decoder: &mut D) -> Result<NonZero<i16>, DecodeError>where
D: BorrowDecoder<'de>,
sourceยงimpl<'de> BorrowDecode<'de> for NonZero<i32>
impl<'de> BorrowDecode<'de> for NonZero<i32>
sourceยงfn borrow_decode<D>(decoder: &mut D) -> Result<NonZero<i32>, DecodeError>where
D: BorrowDecoder<'de>,
fn borrow_decode<D>(decoder: &mut D) -> Result<NonZero<i32>, DecodeError>where
D: BorrowDecoder<'de>,
sourceยงimpl<'de> BorrowDecode<'de> for NonZero<i64>
impl<'de> BorrowDecode<'de> for NonZero<i64>
sourceยงfn borrow_decode<D>(decoder: &mut D) -> Result<NonZero<i64>, DecodeError>where
D: BorrowDecoder<'de>,
fn borrow_decode<D>(decoder: &mut D) -> Result<NonZero<i64>, DecodeError>where
D: BorrowDecoder<'de>,
sourceยงimpl<'de> BorrowDecode<'de> for NonZero<i8>
impl<'de> BorrowDecode<'de> for NonZero<i8>
sourceยงfn borrow_decode<D>(decoder: &mut D) -> Result<NonZero<i8>, DecodeError>where
D: BorrowDecoder<'de>,
fn borrow_decode<D>(decoder: &mut D) -> Result<NonZero<i8>, DecodeError>where
D: BorrowDecoder<'de>,
sourceยงimpl<'de> BorrowDecode<'de> for NonZero<isize>
impl<'de> BorrowDecode<'de> for NonZero<isize>
sourceยงfn borrow_decode<D>(decoder: &mut D) -> Result<NonZero<isize>, DecodeError>where
D: BorrowDecoder<'de>,
fn borrow_decode<D>(decoder: &mut D) -> Result<NonZero<isize>, DecodeError>where
D: BorrowDecoder<'de>,
sourceยงimpl<'de> BorrowDecode<'de> for NonZero<u128>
impl<'de> BorrowDecode<'de> for NonZero<u128>
sourceยงfn borrow_decode<D>(decoder: &mut D) -> Result<NonZero<u128>, DecodeError>where
D: BorrowDecoder<'de>,
fn borrow_decode<D>(decoder: &mut D) -> Result<NonZero<u128>, DecodeError>where
D: BorrowDecoder<'de>,
sourceยงimpl<'de> BorrowDecode<'de> for NonZero<u16>
impl<'de> BorrowDecode<'de> for NonZero<u16>
sourceยงfn borrow_decode<D>(decoder: &mut D) -> Result<NonZero<u16>, DecodeError>where
D: BorrowDecoder<'de>,
fn borrow_decode<D>(decoder: &mut D) -> Result<NonZero<u16>, DecodeError>where
D: BorrowDecoder<'de>,
sourceยงimpl<'de> BorrowDecode<'de> for NonZero<u32>
impl<'de> BorrowDecode<'de> for NonZero<u32>
sourceยงfn borrow_decode<D>(decoder: &mut D) -> Result<NonZero<u32>, DecodeError>where
D: BorrowDecoder<'de>,
fn borrow_decode<D>(decoder: &mut D) -> Result<NonZero<u32>, DecodeError>where
D: BorrowDecoder<'de>,
sourceยงimpl<'de> BorrowDecode<'de> for NonZero<u64>
impl<'de> BorrowDecode<'de> for NonZero<u64>
sourceยงfn borrow_decode<D>(decoder: &mut D) -> Result<NonZero<u64>, DecodeError>where
D: BorrowDecoder<'de>,
fn borrow_decode<D>(decoder: &mut D) -> Result<NonZero<u64>, DecodeError>where
D: BorrowDecoder<'de>,
sourceยงimpl<'de> BorrowDecode<'de> for NonZero<u8>
impl<'de> BorrowDecode<'de> for NonZero<u8>
sourceยงfn borrow_decode<D>(decoder: &mut D) -> Result<NonZero<u8>, DecodeError>where
D: BorrowDecoder<'de>,
fn borrow_decode<D>(decoder: &mut D) -> Result<NonZero<u8>, DecodeError>where
D: BorrowDecoder<'de>,
sourceยงimpl<'de> BorrowDecode<'de> for NonZero<usize>
impl<'de> BorrowDecode<'de> for NonZero<usize>
sourceยงfn borrow_decode<D>(decoder: &mut D) -> Result<NonZero<usize>, DecodeError>where
D: BorrowDecoder<'de>,
fn borrow_decode<D>(decoder: &mut D) -> Result<NonZero<usize>, DecodeError>where
D: BorrowDecoder<'de>,
1.28.0 ยท sourceยงimpl<T> Clone for NonZero<T>where
T: ZeroablePrimitive,
impl<T> Clone for NonZero<T>where
T: ZeroablePrimitive,
sourceยงimpl<'de> Deserialize<'de> for NonZero<i128>
impl<'de> Deserialize<'de> for NonZero<i128>
sourceยงfn deserialize<D>(
deserializer: D,
) -> Result<NonZero<i128>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<NonZero<i128>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
sourceยงimpl<'de> Deserialize<'de> for NonZero<i16>
impl<'de> Deserialize<'de> for NonZero<i16>
sourceยงfn deserialize<D>(
deserializer: D,
) -> Result<NonZero<i16>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<NonZero<i16>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
sourceยงimpl<'de> Deserialize<'de> for NonZero<i32>
impl<'de> Deserialize<'de> for NonZero<i32>
sourceยงfn deserialize<D>(
deserializer: D,
) -> Result<NonZero<i32>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<NonZero<i32>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
sourceยงimpl<'de> Deserialize<'de> for NonZero<i64>
impl<'de> Deserialize<'de> for NonZero<i64>
sourceยงfn deserialize<D>(
deserializer: D,
) -> Result<NonZero<i64>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<NonZero<i64>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
sourceยงimpl<'de> Deserialize<'de> for NonZero<i8>
impl<'de> Deserialize<'de> for NonZero<i8>
sourceยงfn deserialize<D>(
deserializer: D,
) -> Result<NonZero<i8>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<NonZero<i8>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
sourceยงimpl<'de> Deserialize<'de> for NonZero<isize>
impl<'de> Deserialize<'de> for NonZero<isize>
sourceยงfn deserialize<D>(
deserializer: D,
) -> Result<NonZero<isize>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<NonZero<isize>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
sourceยงimpl<'de> Deserialize<'de> for NonZero<u128>
impl<'de> Deserialize<'de> for NonZero<u128>
sourceยงfn deserialize<D>(
deserializer: D,
) -> Result<NonZero<u128>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<NonZero<u128>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
sourceยงimpl<'de> Deserialize<'de> for NonZero<u16>
impl<'de> Deserialize<'de> for NonZero<u16>
sourceยงfn deserialize<D>(
deserializer: D,
) -> Result<NonZero<u16>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<NonZero<u16>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
sourceยงimpl<'de> Deserialize<'de> for NonZero<u32>
impl<'de> Deserialize<'de> for NonZero<u32>
sourceยงfn deserialize<D>(
deserializer: D,
) -> Result<NonZero<u32>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<NonZero<u32>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
sourceยงimpl<'de> Deserialize<'de> for NonZero<u64>
impl<'de> Deserialize<'de> for NonZero<u64>
sourceยงfn deserialize<D>(
deserializer: D,
) -> Result<NonZero<u64>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<NonZero<u64>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
sourceยงimpl<'de> Deserialize<'de> for NonZero<u8>
impl<'de> Deserialize<'de> for NonZero<u8>
sourceยงfn deserialize<D>(
deserializer: D,
) -> Result<NonZero<u8>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<NonZero<u8>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
sourceยงimpl<'de> Deserialize<'de> for NonZero<usize>
impl<'de> Deserialize<'de> for NonZero<usize>
sourceยงfn deserialize<D>(
deserializer: D,
) -> Result<NonZero<usize>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<NonZero<usize>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
1.79.0 ยท sourceยงimpl DivAssign<NonZero<u128>> for u128
impl DivAssign<NonZero<u128>> for u128
sourceยงfn div_assign(&mut self, other: NonZero<u128>)
fn div_assign(&mut self, other: NonZero<u128>)
This operation rounds towards zero, truncating any fractional part of the exact result, and cannot panic.
1.79.0 ยท sourceยงimpl DivAssign<NonZero<u16>> for u16
impl DivAssign<NonZero<u16>> for u16
sourceยงfn div_assign(&mut self, other: NonZero<u16>)
fn div_assign(&mut self, other: NonZero<u16>)
This operation rounds towards zero, truncating any fractional part of the exact result, and cannot panic.
1.79.0 ยท sourceยงimpl DivAssign<NonZero<u32>> for u32
impl DivAssign<NonZero<u32>> for u32
sourceยงfn div_assign(&mut self, other: NonZero<u32>)
fn div_assign(&mut self, other: NonZero<u32>)
This operation rounds towards zero, truncating any fractional part of the exact result, and cannot panic.
1.79.0 ยท sourceยงimpl DivAssign<NonZero<u64>> for u64
impl DivAssign<NonZero<u64>> for u64
sourceยงfn div_assign(&mut self, other: NonZero<u64>)
fn div_assign(&mut self, other: NonZero<u64>)
This operation rounds towards zero, truncating any fractional part of the exact result, and cannot panic.
1.79.0 ยท sourceยงimpl DivAssign<NonZero<u8>> for u8
impl DivAssign<NonZero<u8>> for u8
sourceยงfn div_assign(&mut self, other: NonZero<u8>)
fn div_assign(&mut self, other: NonZero<u8>)
This operation rounds towards zero, truncating any fractional part of the exact result, and cannot panic.
1.79.0 ยท sourceยงimpl DivAssign<NonZero<usize>> for usize
impl DivAssign<NonZero<usize>> for usize
sourceยงfn div_assign(&mut self, other: NonZero<usize>)
fn div_assign(&mut self, other: NonZero<usize>)
This operation rounds towards zero, truncating any fractional part of the exact result, and cannot panic.
1.31.0 ยท sourceยงimpl<T> From<NonZero<T>> for Twhere
T: ZeroablePrimitive,
impl<T> From<NonZero<T>> for Twhere
T: ZeroablePrimitive,
1.28.0 ยท sourceยงimpl<T> Ord for NonZero<T>where
T: ZeroablePrimitive + Ord,
impl<T> Ord for NonZero<T>where
T: ZeroablePrimitive + Ord,
1.28.0 ยท sourceยงimpl<T> PartialEq for NonZero<T>where
T: ZeroablePrimitive + PartialEq,
impl<T> PartialEq for NonZero<T>where
T: ZeroablePrimitive + PartialEq,
1.28.0 ยท sourceยงimpl<T> PartialOrd for NonZero<T>where
T: ZeroablePrimitive + PartialOrd,
impl<T> PartialOrd for NonZero<T>where
T: ZeroablePrimitive + PartialOrd,
sourceยงfn le(&self, other: &NonZero<T>) -> bool
fn le(&self, other: &NonZero<T>) -> bool
self and other) and is used by the <=
operator. Read more1.79.0 ยท sourceยงimpl RemAssign<NonZero<u128>> for u128
impl RemAssign<NonZero<u128>> for u128
sourceยงfn rem_assign(&mut self, other: NonZero<u128>)
fn rem_assign(&mut self, other: NonZero<u128>)
This operation satisfies n % d == n - (n / d) * d, and cannot panic.
1.79.0 ยท sourceยงimpl RemAssign<NonZero<u16>> for u16
impl RemAssign<NonZero<u16>> for u16
sourceยงfn rem_assign(&mut self, other: NonZero<u16>)
fn rem_assign(&mut self, other: NonZero<u16>)
This operation satisfies n % d == n - (n / d) * d, and cannot panic.
1.79.0 ยท sourceยงimpl RemAssign<NonZero<u32>> for u32
impl RemAssign<NonZero<u32>> for u32
sourceยงfn rem_assign(&mut self, other: NonZero<u32>)
fn rem_assign(&mut self, other: NonZero<u32>)
This operation satisfies n % d == n - (n / d) * d, and cannot panic.
1.79.0 ยท sourceยงimpl RemAssign<NonZero<u64>> for u64
impl RemAssign<NonZero<u64>> for u64
sourceยงfn rem_assign(&mut self, other: NonZero<u64>)
fn rem_assign(&mut self, other: NonZero<u64>)
This operation satisfies n % d == n - (n / d) * d, and cannot panic.
1.79.0 ยท sourceยงimpl RemAssign<NonZero<u8>> for u8
impl RemAssign<NonZero<u8>> for u8
sourceยงfn rem_assign(&mut self, other: NonZero<u8>)
fn rem_assign(&mut self, other: NonZero<u8>)
This operation satisfies n % d == n - (n / d) * d, and cannot panic.
1.79.0 ยท sourceยงimpl RemAssign<NonZero<usize>> for usize
impl RemAssign<NonZero<usize>> for usize
sourceยงfn rem_assign(&mut self, other: NonZero<usize>)
fn rem_assign(&mut self, other: NonZero<usize>)
This operation satisfies n % d == n - (n / d) * d, and cannot panic.
sourceยงimpl Serialize for NonZero<i128>
impl Serialize for NonZero<i128>
sourceยงfn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
sourceยงimpl Serialize for NonZero<i16>
impl Serialize for NonZero<i16>
sourceยงfn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
sourceยงimpl Serialize for NonZero<i32>
impl Serialize for NonZero<i32>
sourceยงfn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
sourceยงimpl Serialize for NonZero<i64>
impl Serialize for NonZero<i64>
sourceยงfn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
sourceยงimpl Serialize for NonZero<i8>
impl Serialize for NonZero<i8>
sourceยงfn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
sourceยงimpl Serialize for NonZero<isize>
impl Serialize for NonZero<isize>
sourceยงfn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
sourceยงimpl Serialize for NonZero<u128>
impl Serialize for NonZero<u128>
sourceยงfn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
sourceยงimpl Serialize for NonZero<u16>
impl Serialize for NonZero<u16>
sourceยงfn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
sourceยงimpl Serialize for NonZero<u32>
impl Serialize for NonZero<u32>
sourceยงfn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
sourceยงimpl Serialize for NonZero<u64>
impl Serialize for NonZero<u64>
sourceยงfn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
sourceยงimpl Serialize for NonZero<u8>
impl Serialize for NonZero<u8>
sourceยงfn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
sourceยงimpl Serialize for NonZero<usize>
impl Serialize for NonZero<usize>
sourceยงfn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
impl<T> Copy for NonZero<T>where
T: ZeroablePrimitive,
impl<T> Eq for NonZero<T>where
T: ZeroablePrimitive + Eq,
impl<T> Freeze for NonZero<T>where
T: ZeroablePrimitive + Freeze,
impl<T> RefUnwindSafe for NonZero<T>where
T: ZeroablePrimitive + RefUnwindSafe,
impl<T> Send for NonZero<T>where
T: ZeroablePrimitive + Send,
impl<T> StructuralPartialEq for NonZero<T>where
T: ZeroablePrimitive + StructuralPartialEq,
impl<T> Sync for NonZero<T>where
T: ZeroablePrimitive + Sync,
impl Unaligned for NonZero<i8>
impl Unaligned for NonZero<u8>
impl<T> Unpin for NonZero<T>where
T: ZeroablePrimitive + Unpin,
impl<T> UnwindSafe for NonZero<T>where
T: ZeroablePrimitive + UnwindSafe,
Blanket Implementationsยง
sourceยงimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
sourceยงfn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
sourceยงimpl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
sourceยงdefault unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)sourceยงimpl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
sourceยงunsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)sourceยงimpl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
sourceยงimpl<T> FmtForward for T
impl<T> FmtForward for T
sourceยงfn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.sourceยงfn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.sourceยงfn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.sourceยงfn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.sourceยงfn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.sourceยงfn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.sourceยงfn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.sourceยงfn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.sourceยงimpl<T> IntoEither for T
impl<T> IntoEither for T
sourceยงfn into_either(self, into_left: bool) -> Either<Self, Self> โ
fn into_either(self, into_left: bool) -> Either<Self, Self> โ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moresourceยงfn into_either_with<F>(self, into_left: F) -> Either<Self, Self> โ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> โ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moresourceยงimpl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
sourceยงfn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
sourceยงfn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moresourceยงfn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moresourceยงfn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
sourceยงfn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
sourceยงfn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.sourceยงfn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.sourceยงfn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.sourceยงimpl<T> Tap for T
impl<T> Tap for T
sourceยงfn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moresourceยงfn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moresourceยงfn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moresourceยงfn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moresourceยงfn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moresourceยงfn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moresourceยงfn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.sourceยงfn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.sourceยงfn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.sourceยงfn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.sourceยงfn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.sourceยงfn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.sourceยงfn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.