Crate cheque [] [src]

Provides a macro make checked math pretty easy.

let a = 10u8;
let b = 20u8;
let_checked![a, b];
assert_eq!(a + b, 30);
assert_eq!(b * b, None);
assert_eq!(b / 0, None);
assert_eq!(a - 20, None);
assert_eq!((a - b) + 1, None);

let_checked! redeclares each identifier as a checked numeric value. You can then use +, etc. on the checked variables, and then deref the result to get an Option<_>.

You can also use numeric literals/unchecked values, so long as they are on the right side of the operation.

let c = 20usize;
if let Some(scary) = *(c - 100) {
    panic!("Ahh! {:?}", scary);

If you are doing generic programming, you should add the checked num_traits to your where bounds.





This implements Deref<Option<T>>.