expression
Decimal checked arithmetic
Check representability or division failure instead of unconditionally unwrapping an arbitrary input. Decimal is finite; operations may round to representable precision. Expected stdout (not executed): 3.75 true true Common mistake: Assuming checked arithmetic guarantees all mathematical results are exact. Dependency: rust_decimal =1.36.0. See the staged Cargo fixture. Exact-version API and resolution not tested.
Formal Syntax & Structure
rust syntaxrust
a.checked_add(b); a.checked_mul(b); a.checked_div(b)Working Example
Example Snippetrust
fn main() { use rust_decimal::Decimal; let price = Decimal::new(125,2); let units = Decimal::new(3,0); println!("{}",price.checked_mul(units).unwrap()); println!("{} {}",Decimal::MAX.checked_add(Decimal::ONE).is_none(),price.checked_div(Decimal::ZERO).is_none());}