checked_sum 0.1.0

A library safely summing up iterators.
Documentation
  • Coverage
  • 100%
    5 out of 5 items documented2 out of 5 items with examples
  • Size
  • Source code size: 6.18 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 266.68 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • elsirion/checked_sum
    2 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • elsirion

checked_sum

Crates.io Docs.rs License

Utility crate for summing up iterators in a safe way. The CheckedSum trait is implemented for any iterator of items implementing CheckedAdd, which in turn is implemented for all integer primitives but can also be implemented for other types like newtypes wrapping integers.

use checked_sum::CheckedSum;

// If the sum fits into the type, it is returned
let numbers = vec![1u8, 2, 3, 4, 5];
assert_eq!(numbers.into_iter().checked_sum(), Some(15),);

// If the sum overflows, `None` is returned
let numbers = vec![255u8, 1];
assert_eq!(numbers.into_iter().checked_sum(), None,);