arith
Plain +, -, * and ** on int panic when the result does not fit. Import one of these traits to say what should happen instead: Checked gives an Option, Wrapping wraps modulo 2^64, Saturating clamps, and Overflowing returns the wrapped value with a flag.
use arith::{Checked, Wrapping};
use tests::{assert};
fn main() {
assert!(9223372036854775807.checked_add(1).is_none());
assert!(31.wrapping_pow(20) != 0);
}
Part of coil-stdlib. spool projects get it automatically; with plain coil, pass --root ~/.coil/stdlib/src.
Import what you use (use path::* is not allowed):
use arith::{Checked, Wrapping};
Traits#
| Name | Description |
|---|---|
Checked | Integer arithmetic that returns None instead of panicking when the result does not fit. |
Wrapping | Integer arithmetic that wraps around modulo 2^64 instead of panicking. |
Saturating | Integer arithmetic that clamps to INT_MIN / INT_MAX instead of panicking. |
Overflowing | Integer arithmetic that returns the wrapped result together with whether it wrapped. |