/safe_arith

Primary LanguageRust

safe_arith

CI Crates.io Documentation

Safe arithmetic on integers, avoiding overflow and division by zero.

Why not just use checked_* methods?

Rust's built-in checked_add, checked_mul, etc. return Option<T>, which requires you to manually handle None cases without context about what went wrong. This crate provides:

  1. Explicit errors: Returns Result<T, ArithError> with explicit Overflow and DivisionByZero variants
  2. Assignment operators: Methods like safe_add_assign that mutate in place
  3. Iterator support: safe_sum() method for safely summing iterators without overflow