[][src]Macro nonzero_ext::nonzero

macro_rules! nonzero {
    ($n:expr) => { ... };
}

Create non-zero values from constant literals easily.

This macro issues a compile-time check and, if it passes, creates the corresponding non-zero numeric value from the given constant. Since the type of constant literals needs to be exactly known, nonzero! requires that you annotate the constant with the type, so instead of nonzero!(20) you must write nonzero!(20 as u16).

Const expressions

This macro can be used in const expressions.

Examples

nonzero!(20usize);  // => NonZeroUsize
nonzero!(20u32);    // => NonZeroU32
nonzero!(20 as u8); // => NonZeroU8

and passing a zero of any type will fail:

This example deliberately fails to compile
nonzero!(0u8);