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.
let mut total = 10; total += 5; total <<= 1; (a, b) = (b, a);
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
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 += 1orf() += 1do not compile (unlessf()returns something you can dereference, like*f() += 1with a&mutresult). - Immutable bindings:
x += 1on a non-mutvariable 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--: writex += 1. Assignment having type()is whywhile (x = next())style loops are impossible; Rust useswhile letinstead. - Moves:
a = bmovesbunless the type isCopy. Assigning over a variable runs the old value'sDropfirst. - Strings:
String += &stris supported;String += Stringis 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 nomutorref. Declare the variables first (let (x, y);). - Compound assignment on references:
*r += 1is needed whenris a&mut i32; the compiler does not auto-dereference the left side of an operator. - Overloading: implement
AddAssignand friends for your own types; they take&mut selfand the right-hand value. See operator overloading.
Related
- Arithmetic operators
- Bitwise operators
- * dereference
- let and mut
- Tutorial: Variables, Operators