5 ms·
It does. There are types in the standard library for non-zero numbers[1]. While it would involve writing a wrapper, Rust does provide for customizing the behavi
by gatherhunterer 7y ago
It does. There are types in the standard library for non-zero numbers[1]. While it would involve writing a wrapper, Rust does provide for customizing the behavior of operators[2]. The example implementation for Div (the "/" operator) even shows how to make a type that panics when trying to divide by zero. One could also return a Result so that trying to divide by zero can fail gracefully.
1. https://doc.rust-lang.org/std/num/struct.NonZeroI32.html https://doc.rust-lang.org/std/num/struct.NonZeroI32.html
2. https://doc.rust-lang.org/std/ops/trait.Div.html https://doc.rust-lang.org/std/ops/trait.Div.html