pub trait CheckedAdd: Sized {
// Required method
fn checked_add(&self, other: &Self) -> Option<Self>;
}Expand description
Numeric type supporting overflow-checked addition.
The trait is implemented for all primitive types and can be implemented
manually for custom newtypes. Please note that CheckedSum also requires
the item type to implement Default.
use checked_sum::{CheckedAdd, CheckedSum};
#[derive(Debug, PartialEq, Default)]
struct MyInt(u32);
impl CheckedAdd for MyInt {
fn checked_add(&self, other: &Self) -> Option<Self> {
self.0.checked_add(other.0).map(MyInt)
}
}
let numbers = vec![MyInt(1), MyInt(2), MyInt(3)];
assert_eq!(numbers.into_iter().checked_sum(), Some(MyInt(6)));Required Methods§
Sourcefn checked_add(&self, other: &Self) -> Option<Self>
fn checked_add(&self, other: &Self) -> Option<Self>
Adds two numbers checking for overflow, returns None if overflow
occurred.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.