NonZeroI8 in std::num - Rust
Description: An `i8` that is known not to equal zero.

## NonZeroI8

# Type Alias NonZeroI8

1.34.0 · Source

    pub type NonZeroI8 = NonZero<i8>;

Expand description

An `i8` that is known not to equal zero.

This enables some memory layout optimization. For example, `Option<NonZeroI8>` is the same size as `i8`:

    assert_eq!(size_of::<Option<core::num::NonZeroI8>>(), size_of::<i8>());

## §Layout

`NonZeroI8` is guaranteed to have the same layout and bit validity as `i8` with the exception that `0` is not a valid instance. `Option<NonZeroI8>` is guaranteed to be compatible with `i8`, including in FFI.

Thanks to the null pointer optimization, `NonZeroI8` and `Option<NonZeroI8>` are guaranteed to have the same size and alignment:

    use std::num::NonZeroI8;

    assert_eq!(size_of::<NonZeroI8>(), size_of::<Option<NonZeroI8>>());
    assert_eq!(align_of::<NonZeroI8>(), align_of::<Option<NonZeroI8>>());

## §Compile-time creation

Since both `Option::unwrap()` and `Option::expect()` are `const`, it is possible to define a new `NonZeroI8` at compile time via:

    use std::num::NonZeroI8;

    const TEN: NonZeroI8 = NonZeroI8::new(10).expect("ten is non-zero");

## Aliased Type§

    pub struct NonZeroI8(/* private fields */);