Checked division prevents division by zero from occurring.
The programmer can then handle the returned std::option::Option.
Using checked division and remainder is particularly important in the signed integer case,
where arithmetic overflow can also occur when dividing the minimum representable value by -1.
fn main() {
// Using the checked division API
let _y = match 5i32.checked_div(0) {
None => 0,
Some(r) => r,
};
// Using the checked remainder API
let _z = match 5i32.checked_rem(0) {
None => 0,
Some(r) => r,
};
}
|