Macro nonzero_ext::nonzero[][src]

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

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).

Note that this macro only works with integer literals, it isn’t possible to use the nonzero! macro with types other than the built-in ones.

Determining the output type

Use a suffix on the input value to determine the output type: nonzero!(1_usize) will return a NonZeroUsize, and nonzero!(-1_i32) will return a NonZeroI32.

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:

nonzero!(0u8);