Thread with 4 posts
jump to expanded postrust question. a common pattern in code i’ve written is:
assert!(foo % bar == 0);
let count = foo / bar;
is there a standard way to do that as a single operation?
one of the things i really like about rust is how it helps you avoid errors by combining an assertion of some assumption (e.g. “foo is not None”) with obtaining a value based on that assumption (e.g. getting the value inside the Some()). so in this case, it’d be really nice if i could do something like foo.div_exact(bar).unwrap(). personally i’d put it in the stdlib…
@hikari you can use the NonZero types for this, depending on context
@knickish could you elaborate a bit?
@hikari If both values not being zero is ok everywhere that you use them (or at least for the denominator), the compiler will guarantee that a NonZeroU32 is, well, not zero. Can mostly use it anywhere you would use a normal u32