Skip to content
Tutorials Center
My learning

Topics

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 +=.

Rust
impl AddAssign for Counter {
    fn add_assign(&mut self, rhs: Counter) { self.0 += rhs.0; }
}

Try it

Rust Implementing += and *=

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.

Rust In-place update of a growing value

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

Rust
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 += b needs AddAssign, not Add. If you only implement Add, a += b fails with "binary assignment operation += cannot be applied". For Copy types a common pattern is *self = *self + rhs; inside the assign impl.
  • Prefer the real in-place update. For heap types, AddAssign can mutate the existing allocation, while a = a + b would move and rebuild.
  • The left side must be a mutable place. A let binding needs mut, 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 += c is not valid because the expression evaluates to ().
  • Standard library: all numeric types, String (+= &str), Duration, Instant (via += with Duration) and Wrapping<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.