std-type

NonZero and NonZeroU8

Represent a nonzero quantity after checking input. The type excludes zero, which is useful for a divisor or a batch size. Expected stdout (not executed): 5 true Common mistake: Assuming nonzero means positive for signed variants.

Formal Syntax & Structure

rust syntaxrust
NonZeroU8::new(value) -> Option<NonZeroU8>; value.get()

Working Example

Example Snippetrust
fn main() {    use std::num::NonZeroU8;    let batch = NonZeroU8::new(4).unwrap();    println!("{} {}", 20 / batch.get(), NonZeroU8::new(0).is_none());}
Rust Reference