Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Operators and Expressions

Rust compound assignment operators

Reference for Rust assignment: =, += -= *= /= %= &= |= ^= <<= >>=, the *Assign traits, destructuring assignment, evaluation order and the unit result.

Plain assignment place = value moves (or copies) a value into an existing place and drops whatever was there. A compound assignment such as x += 1 combines an operator with assignment: for primitive types it behaves like x = x + 1, but it is implemented through a separate trait (AddAssign, SubAssign, ...) that can modify the left side in place, which matters for types like String and Vec.

Assignment is an expression, but its value is always the unit value (), so you cannot chain a = b = c and there is no x++. The left side must be a mutable place: a mut variable, a field, an index expression or a dereference of a &mut.

Rust
let mut total = 10;
total += 5;
total <<= 1;
(a, b) = (b, a);

Try it

Rust All the compound operators

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 Destructuring assignment and swapping

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
place = value
place op= value        // op is one of + - * / % & | ^ << >>
(a, b) = (b, a)        // destructuring assignment (Rust 1.59)
Operator Trait (in std::ops) Typical meaning
+= AddAssign add (or append for String)
-= SubAssign subtract
*= MulAssign multiply
/= DivAssign divide (panics on integer division by zero)
%= RemAssign remainder
&= BitAndAssign bitwise AND
|= BitOrAssign bitwise OR
^= BitXorAssign bitwise XOR
<<= ShlAssign shift left
>>= ShrAssign shift right

Returns: () in every case.

Panics: the same as the plain operator: arithmetic overflow in debug builds, division or remainder by zero, and shifts by too many bits.

Usage notes

  • Left side must be a place: 5 += 1 or f() += 1 do not compile (unless f() returns something you can dereference, like *f() += 1 with a &mut result).
  • Immutable bindings: x += 1 on a non-mut variable is error E0384; a field behind a & reference gives E0594.
  • Evaluation order: if both operands are primitive integers or floats, the right side is evaluated first. For other types, the left side is evaluated first like a method call. Do not write code that depends on it.
  • No ++ or --: write x += 1. Assignment having type () is why while (x = next()) style loops are impossible; Rust uses while let instead.
  • Moves: a = b moves b unless the type is Copy. Assigning over a variable runs the old value's Drop first.
  • Strings: String += &str is supported; String += String is not (take a reference), and &str += ... does not exist.
  • Destructuring assignment: the pattern on the left may contain tuples, arrays, slices, tuple structs, structs, _ and .., but no new bindings and no mut or ref. Declare the variables first (let (x, y);).
  • Compound assignment on references: *r += 1 is needed when r is a &mut i32; the compiler does not auto-dereference the left side of an operator.
  • Overloading: implement AddAssign and friends for your own types; they take &mut self and the right-hand value. See operator overloading.