You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Rust language team decided not to add CheckedSum and CheckedProduct traits to core rust-lang/rust#95485 but it was suggested that they be added here.
These traits allow you to add (or multiply) together all numbers in an iterator but return None instead of panicking or wrapping if an overflow occurs.
I think the best way to do it is with a blanket implementation:
pubtraitCheckedSum<A = Self>:Sized{fnchecked_sum<I:Iterator<Item = A>>(iter:I) -> Option<Self>;}impl<T>CheckedSum<T>forTwhereT:CheckedAdd + Sized + crate::identities::Zero{fnchecked_sum<I:Iterator<Item = Self>>(iter:I) -> Option<Self>{letmut total = Self::zero();for i in iter{
total = total.checked_add(&i)?;}Some(total)}}
I will submit a pull request and if that is accepted I might do the same for Overflowing, Saturating, and Wrapping
The text was updated successfully, but these errors were encountered:
The Rust language team decided not to add
CheckedSum
andCheckedProduct
traits to core rust-lang/rust#95485 but it was suggested that they be added here.These traits allow you to add (or multiply) together all numbers in an iterator but return
None
instead of panicking or wrapping if an overflow occurs.I think the best way to do it is with a blanket implementation:
I will submit a pull request and if that is accepted I might do the same for Overflowing, Saturating, and Wrapping
The text was updated successfully, but these errors were encountered: