diff --git a/src/lib.rs b/src/lib.rs index 698e86f..06d39af 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ use std::fmt; -use std::ops::Add; +use std::ops::{Add, AddAssign}; /// This enum represents the sum of a sequence of numbers that may be integers or floating point. /// Integer is the default. When a floating point number is added to the sum, the type is converted @@ -24,6 +24,13 @@ impl Add for Sum { } } +impl AddAssign for Sum { + /// Adds two Sums. If either is a Float, the result will be a Float. + fn add_assign(&mut self, other: Self) { + *self = *self + other; + } +} + impl fmt::Display for Sum { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { @@ -51,6 +58,10 @@ mod tests { let a = Sum::Integer(1); let b = Sum::Integer(2); assert_eq!(a + b, Sum::Integer(3)); + + let mut c = a; + c += b; + assert_eq!(c, Sum::Integer(3)); } #[test] diff --git a/src/main.rs b/src/main.rs index a7cfea3..514ee76 100644 --- a/src/main.rs +++ b/src/main.rs @@ -79,7 +79,7 @@ fn main() -> std::result::Result<(), Box> { } } }; - sum = sum + n; + sum += n; log::debug!("{i}: col={col:?}, n={n:?}, sum={sum:?}"); } }