Compiling playground v0.0.1 (/playground)
error[E0369]: cannot divide `NonZero<{integer}>` by `NonZero<{integer}>`
--> src/main.rs:6:20
|
6 | let div = four / two;
| ---- ^ --- NonZero<{integer}>
| |
| NonZero<{integer}>
|
note: the foreign item type `NonZero<{integer}>` doesn't implement `Div<NonZero<{integer}>>`
--> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/num/nonzero.rs:115:1
|
115 | pub struct NonZero<T: ZeroablePrimitive>(T::NonZeroInner);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not implement `Div<NonZero<{integer}>>`
For more information about this error, try `rustc --explain E0369`.
error: could not compile `playground` (bin "playground") due to 1 previous error
I see thereโs a trait implementation for unsigned types, like u๐ / NonZero<u๐> with result u๐, so impl Div<NonZero<u๐>> for u๐ { type Output = u๐; โฆ }. The rationale for providing this impl is probably that for those, the division can avoid overflow-checks, so using this impl improves efficiency.
For unsigned types u๐ (u8, u16, etcโฆ) and two variables, bothNonZero<u๐> like x: NonZero<u๐>; y: NonZero<u๐>, you can thus do division with x.get() / y, converting just the left-hand-side, and still benefiting from this more efficient implementation.
For signed types i๐ (i8, i16, etcโฆ) however, thereโs another overflow case: i๐::MIN / -1. Since overflow is possible anyway, itโs probably expected that you might as well just use i๐ values from the beginning; e.g. x.get() / y.get().