Rust Reference ยท Core Traits
Compound assignment traits
Reference for Rust compound assignment traits: AddAssign, SubAssign, MulAssign, DivAssign, RemAssign and bitwise variants, in-place updates, and why += needs its own impl.
Compound assignment operators such as += and <<= have their own traits in std::ops: AddAssign, SubAssign, MulAssign, DivAssign, RemAssign, BitAndAssign, BitOrAssign, BitXorAssign, ShlAssign and ShrAssign. Each has one method that takes &mut self and updates the value in place.
They are separate from Add and the others on purpose. In-place updates can reuse existing storage and avoid creating a new value, which matters for types like big numbers, strings and matrices. Implementing Add does not give you +=.
impl AddAssign for Counter { fn add_assign(&mut self, rhs: Counter) { self.0 += rhs.0; } }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
pub trait AddAssign<Rhs = Self> { fn add_assign(&mut self, rhs: Rhs); } // SubAssign::sub_assign, MulAssign::mul_assign, DivAssign::div_assign, // RemAssign::rem_assign, BitAndAssign::bitand_assign, BitOrAssign::bitor_assign, // BitXorAssign::bitxor_assign, ShlAssign::shl_assign, ShrAssign::shr_assign
| Name | Description |
|---|---|
Rhs |
Right operand type, defaulting to Self. |
&mut self |
The left operand, modified in place. |
rhs |
The right operand, taken by value. |
Returns: nothing (()). The expression a += b itself has type ().
Errors / panics / edge cases: built-in integer += panics on overflow in debug builds. Division and remainder assignment panic on a zero divisor for integers.
Usage notes
a += bneedsAddAssign, notAdd. If you only implementAdd,a += bfails with "binary assignment operation+=cannot be applied". ForCopytypes a common pattern is*self = *self + rhs;inside the assign impl.- Prefer the real in-place update. For heap types,
AddAssigncan mutate the existing allocation, whilea = a + bwould move and rebuild. - The left side must be a mutable place. A
letbinding needsmut, and through a reference you write*r += 1. - Right operand is moved. Use
AddAssign<&T>as well when callers should not have to clone. - Implement the binary operator via the assign one (as in the second example) to keep behavior in sync and avoid duplicate code.
- No chaining.
a += b += cis not valid because the expression evaluates to(). - Standard library: all numeric types,
String(+= &str),Duration,Instant(via+=withDuration) andWrapping<T>implement the relevant traits. - Evaluation order: for primitive types the right operand is evaluated first; for overloaded types the left place is evaluated first. Avoid side effects in both operands.