3 ms·
Rust has your back: pub fn checked_div(self, rhs: u8) -> Option<u8> Checked integer division. Computes self / rhs, returning None if rhs == 0. You would use
by arcticbull 8y ago
Rust has your back:
pub fn checked_div(self, rhs: u8) -> Option<u8>
Checked integer division. Computes self / rhs, returning None if rhs == 0.
You would use this as lhs.checked_div(rhs).unwrap_or(your sane default)
This is dramatically better than always returning zero silently, as doing so is bound to be wrong in certain cases. If you run into a situation where you are afraid your RHS may be 0 but still want to do the right thing, this is what you'd use.