Idiomatic/recommended way to chain checked arithmetic

What the idiomatic/recommended way to chain multiple checked_foo arithmetic operations when a function computes an expression that contains more than one arithmetic operation?

Suppose the non-checked form is return (a + 1) * (3 + b). What the idiomatic/recommended way to write it with checked arithmetic?

2 Likes

While this doesn't help you today, with RFC #1859 it'd be

fn foo(a: i32, b: i32) -> Option<i32> {
    Some(a.checked_add(1)?.checked_mul(3.checked_add(b)?)?)
}

EDIT 6 years later: This is stable! (And has been for a while.)

1 Like