Rust

E0368 Assignment Operation Cannot Apply Fix

E0368 is a missing assignment trait, not a missing operator: the plain + may work, or the type may support the operation for other values, but the specific compound form - this left-hand type, this operator, this right-hand type - has no implementation to call. The classic case is a custom type that implements Add and still rejects +=, because AddAssign is not implemented automatically. This guide works the shapes that produce E0368, shows the repairs running, and separates the neighbors that share the same line of code but carry different codes: E0369 for the plain operator, E0277 and E0308 for some mismatched right-hand types, and the assignment-place errors E0384, E0381, and E0594.

Written November 30, 2026 · Tested with Rust 1.99.1 · 16 min read

By the end of this guide
  • The rule underneath: compound assignment calls the assignment trait
  • How to read the diagnostic
  • The first failure: a float shifted with <<=
  • The custom-type failure: Add is not AddAssign

Written November 30, 2026

Tested with Rust 1.99.1

What this guide is

Error E0368 is a compound assignment with no implementation behind it. The error codes index opens with the cause - "A binary assignment operator like += or ^= was applied to a type that doesn't support it" - and its erroneous example is the first failure in this guide: a f32 shifted with <<=. Its stated repair direction is to check that the type implements the binary operation, and its second example names the trap this guide is built around: a type that implements Add still rejects +=, because "AddAssign is not automatically implemented, so you need to manually implement it for your type."

The practical payoff is reading the code on the diagnostic before choosing a repair, because the same += line can fail four different ways in this guide's probed set. When the left-hand type has no assignment implementation for the operator at all, the code is E0368. When the left-hand type does implement the assignment trait, but not for that right-hand type, the probed numeric cases emitted E0277 and the probed String cases emitted E0308 instead. When the operation is fine but the place being assigned to is not writable or not yet initialized, the code is an assignment-place error - E0384, E0381, or E0594 in the probed set. And the plain operator without the = is a different guide entirely: it is E0369, covered there and reproduced here only to draw the line.

Build every program in this guide with rustc --edition 2021. In the compiler transcripts below, the file names are the bare names shown on the command line.

The rule underneath: compound assignment calls the assignment trait

The Rust Reference's operator expressions section defines the form: "Compound assignment expressions combine arithmetic and logical binary operators with assignment expressions", and "the syntax of compound assignment is a mutable place expression, the assigned operand, then one of the operators followed by an = as a single token (no whitespace), and then a value expression, the modifying operand." Outside the primitive case it describes, "this expression is syntactic sugar for using the corresponding trait for the operator" and calling its method with the left-hand side as the receiver - so x += y calls AddAssign::add_assign. Its operator table pairs each plain trait with a separate compound trait: + with std::ops::Add and std::ops::AddAssign, << with std::ops::Shl and std::ops::ShlAssign, and so on down the table.

That pairing is the whole error. Add and AddAssign are two traits, and implementing one does not implement the other - the error index says so in the sentence quoted above. A type pair supports += only when the exact AddAssign implementation for that left-hand and right-hand combination exists. The AddAssign documentation shows how specific those implementations are: alongside the same-type implementations for the primitive numeric types, it lists impl AddAssign<&str> for String - "Implements the += operator for appending to a String" with "the same behavior as the push_str method" - and no AddAssign<String> or AddAssign<char> for String in that list, which is exactly the split the String programs below land on.

How to read the diagnostic

The E0368 header names the operator and the left-hand type: "binary assignment operation += cannot be applied to type Foo", and the span underlines the whole compound expression with the label "cannot use += on type Foo". For a custom type, two notes follow in the probed set: one points at the type definition - "an implementation of AddAssign might be missing for Foo" and "must implement AddAssign" - and one points at the trait definition itself, "the trait AddAssign must be implemented". When the right-hand side is a reference, the missing implementation named in the note is the reference form: the Foo program with &Foo on the right names AddAssign<&Foo>, not AddAssign. Read that note literally - it tells you the exact trait implementation to write if you choose the implement-the-trait repair. The other codes in this guide read differently: E0277 and E0308 point at the right-hand operand alone, and the assignment-place errors point at the place or the binding, so the span position is your first clue to which playbook you are in.

The first failure: a float shifted with <<=

This is the error index's own example, as a complete program - a f32 shifted left in place:

Rust
fn main() {
    let mut x = 12f32;
    x <<= 2;
    println!("{}", x);
}
Output
error[E0368]: binary assignment operation `<<=` cannot be applied to type `f32`
 --> q1.rs:3:5
  |
3 |     x <<= 2;
  |     -^^^^^^
  |     |
  |     cannot use `<<=` on type `f32`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0368`.

Floating-point types have no shift operation, plain or compound, in the probed set - the plain form of this same operation is the E0369 neighbor below. The same left-hand type with a different unsupported compound operator fails identically in structure: x ^= 2 on a f32 emitted E0368 for ^= in the probed set. The index's repair for this shape is not a trait implementation - you cannot implement ShlAssign for f32, a type from the standard library - but a change of type: its fixed example switches the value to u32, which "does implement the ShlAssign trait". That fix is f1 below.

A bool fails the same way for +=, with the same header and span shape and no missing-implementation note, because there is no user type definition to point at:

Rust
fn main() {
    let mut x = true;
    x += true;
    println!("{}", x);
}
Output
error[E0368]: binary assignment operation `+=` cannot be applied to type `bool`
 --> q5.rs:3:5
  |
3 |     x += true;
  |     -^^^^^^^^
  |     |
  |     cannot use `+=` on type `bool`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0368`.

So does a Vec given a single element with += - a standard-library container this time, with a note saying the type "does not implement AddAssign<{integer}>":

Rust
fn main() {
    let mut v = vec![1, 2];
    v += 3;
    println!("{:?}", v);
}
Output
error[E0368]: binary assignment operation `+=` cannot be applied to type `Vec<{integer}>`
   --> q7.rs:3:5
    |
  3 |     v += 3;
    |     -^^^^^
    |     |
    |     cannot use `+=` on type `Vec<{integer}>`
    |
note: `Vec<{integer}>` does not implement `AddAssign<{integer}>`
   --> /home/droopy/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:436:1
    |
436 | pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Vec<{integer}>` is defined in another crate

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0368`.

For built-in and standard-library types, E0368 in the probed set means the operation itself is not on offer for that type - the repair is a different operation or a different type, never an implementation you write. The custom-type case, next, is the opposite.

The custom-type failure: Add is not AddAssign

This is the error index's second example, as a complete program - a struct with a working Add implementation, used with +=:

Rust
use std::ops::Add;
struct Foo(u32);
impl Add for Foo {
    type Output = Foo;
    fn add(self, rhs: Foo) -> Foo {
        Foo(self.0 + rhs.0)
    }
}
fn main() {
    let mut x = Foo(5);
    x += Foo(7);
    println!("{}", x.0);
}
Output
error[E0368]: binary assignment operation `+=` cannot be applied to type `Foo`
   --> q2.rs:11:5
    |
 11 |     x += Foo(7);
    |     -^^^^^^^^^^
    |     |
    |     cannot use `+=` on type `Foo`
    |
note: an implementation of `AddAssign` might be missing for `Foo`
   --> q2.rs:2:1
    |
  2 | struct Foo(u32);
    | ^^^^^^^^^^ must implement `AddAssign`
note: the trait `AddAssign` must be implemented
   --> /home/droopy/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/arith.rs:768:1
    |
768 | pub const trait AddAssign<Rhs = Self> {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0368`.

The Add implementation is real - f3 below runs the same type through x = x + Foo(7) and prints the sum - but += never consults it. Removing the Add implementation entirely changes nothing about the code: the same program with no operator traits at all emits the same E0368, with the same missing-AddAssign notes:

Rust
struct Foo(u32);
fn main() {
    let mut x = Foo(5);
    x += Foo(7);
    println!("{}", x.0);
}
Output
error[E0368]: binary assignment operation `+=` cannot be applied to type `Foo`
   --> q3.rs:4:5
    |
  4 |     x += Foo(7);
    |     -^^^^^^^^^^
    |     |
    |     cannot use `+=` on type `Foo`
    |
note: an implementation of `AddAssign` might be missing for `Foo`
   --> q3.rs:1:1
    |
  1 | struct Foo(u32);
    | ^^^^^^^^^^ must implement `AddAssign`
note: the trait `AddAssign` must be implemented
   --> /home/droopy/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/arith.rs:768:1
    |
768 | pub const trait AddAssign<Rhs = Self> {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0368`.

The right-hand type is part of the missing implementation, not a detail. Giving += a reference on the right - &Foo instead of Foo - still emits E0368, but the note now names the reference form of the trait:

Rust
struct Foo(u32);
fn main() {
    let mut x = Foo(5);
    x += &Foo(7);
    println!("{}", x.0);
}
Output
error[E0368]: binary assignment operation `+=` cannot be applied to type `Foo`
   --> q11.rs:4:5
    |
  4 |     x += &Foo(7);
    |     -^^^^^^^^^^^
    |     |
    |     cannot use `+=` on type `Foo`
    |
note: an implementation of `AddAssign<&Foo>` might be missing for `Foo`
   --> q11.rs:1:1
    |
  1 | struct Foo(u32);
    | ^^^^^^^^^^ must implement `AddAssign<&Foo>`
note: the trait `AddAssign` must be implemented
   --> /home/droopy/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/arith.rs:768:1
    |
768 | pub const trait AddAssign<Rhs = Self> {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0368`.

So an AddAssign implementation for one right-hand type does not cover another in the probed set: the owned form and the reference form are separate implementations, and f8 below shows the reference form implemented and running on its own. Do not generalize that past the probed pair - it shows Foo needs the implementation matching the right-hand type you actually use, in these two forms.

The traps: neighbors that are not E0368

Trap
Five nearby shapes fail under a different code, or for a different reason.

The plain operator on the same unsupported pair is E0369 - the subject of the E0369 guide, not this one. Dropping the = from the first failure turns E0368 into E0369, with a different header - "no implementation for f32 << {integer}" - and a span on the operator alone:

Rust
fn main() {
    let x = 12f32;
    println!("{}", x << 2);
}
Output
error[E0369]: no implementation for `f32 << {integer}`
 --> n1.rs:3:22
  |
3 |     println!("{}", x << 2);
  |                    - ^^ - {integer}
  |                    |
  |                    f32

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0369`.

The same split holds for the custom type: the plain + on a Foo with no Add implementation is E0369 naming Add, where the compound form above was E0368 naming AddAssign:

Rust
struct Foo(u32);
fn main() {
    let x = Foo(5);
    let y = x + Foo(7);
    println!("{}", y.0);
}
Output
error[E0369]: cannot add `Foo` to `Foo`
  --> n2.rs:4:15
   |
 4 |     let y = x + Foo(7);
   |             - ^ ------ Foo
   |             |
   |             Foo
   |
note: an implementation of `Add` might be missing for `Foo`
  --> n2.rs:1:1
   |
 1 | struct Foo(u32);
   | ^^^^^^^^^^ must implement `Add`
note: the trait `Add` must be implemented
  --> /home/droopy/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/arith.rs:76:1
   |
76 | pub const trait Add<Rhs = Self> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0369`.

The rule of thumb in the probed set is exact: the operator with = is this guide, the operator without it is E0369. If your diagnostic says E0369, implementing an assignment trait will not touch it.

A cross-type numeric compound assignment is not E0368 either. An i32 does implement AddAssign - for i32 and for &i32 - but not for f64, and that narrower miss emitted E0277, "cannot add-assign f64 to i32", with the span on the += alone and help text naming the unimplemented trait combination:

Rust
fn main() {
    let mut x: i32 = 5;
    x += 1.5f64;
    println!("{}", x);
}
Output
error[E0277]: cannot add-assign `f64` to `i32`
   --> q4.rs:3:7
    |
  3 |     x += 1.5f64;
    |       ^^ no implementation for `i32 += f64`
    |
    = help: the trait `AddAssign<f64>` is not implemented for `i32`
help: `i32` implements trait `AddAssign<Rhs>`
   --> /home/droopy/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/arith.rs:786:9
    |
786 |         const impl AddAssign for $t {
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `AddAssign`
...
799 | add_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f16 f32 f64 f128 }
    | ----------------------------------------------------------------------------------------- in this macro invocation
    |
   ::: /home/droopy/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/internal_macros.rs:61:9
    |
 61 |         const impl $imp<&$u> for $t {
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `AddAssign<&i32>`
    = note: this error originates in the macro `add_assign_impl` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.

The i32 += f32 pairing emitted the same E0277 in the probed set. Do not read this as "a mismatched right-hand type is always E0277" - the next shape is the counterexample. The repair here is a conversion, not a trait: bring the right-hand value to the left-hand type (or change the left-hand type) so an implementation that exists is the one being asked for, as f5 shows with matched types.

A String compound assignment with the wrong right-hand type is E0308, "mismatched types". String implements AddAssign<&str>, and the compiler holds the right-hand side to exactly that: an owned String on the right is rejected with a help suggesting the borrow:

Rust
fn main() {
    let mut s = String::from("a");
    s += String::from("b");
    println!("{}", s);
}
Output
error[E0308]: mismatched types
 --> q8.rs:3:10
  |
3 |     s += String::from("b");
  |          ^^^^^^^^^^^^^^^^^ expected `&str`, found `String`
  |
help: consider borrowing here
  |
3 |     s += &String::from("b");
  |          +

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.

An integer on the right fails the same way, expecting &str:

Rust
fn main() {
    let mut s = String::from("a");
    s += 5;
    println!("{}", s);
}
Output
error[E0308]: mismatched types
 --> q6.rs:3:10
  |
3 |     s += 5;
  |          ^ expected `&str`, found integer

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.

A char on the right also emitted E0308 in the probed set, expecting &str and suggesting the string literal. So in the probed set the E0368-versus-not line is not "custom versus built-in" and not "matching versus mismatched": bool, Vec, and the custom Foo - types with no AddAssign for the operator in play - emitted E0368, while i32 with a float right-hand side emitted E0277 and String with a non-&str right-hand side emitted E0308, both types that do have an AddAssign implementation, just not for that right-hand type. That is a pattern across this guide's probed set, stated for these pairings - read your own diagnostic's code rather than predicting it from the shape.

The last three neighbors are not about the operation at all - the place being assigned to is the problem, and each has its own code. Compound assignment to an immutable binding is E0384:

Rust
fn main() {
    let x = 5;
    x += 1;
    println!("{}", x);
}
Output
error[E0384]: cannot assign twice to immutable variable `x`
 --> n4.rs:3:5
  |
2 |     let x = 5;
  |         - first assignment to `x`
3 |     x += 1;
  |     ^^^^^^ cannot assign twice to immutable variable
  |
help: consider making this binding mutable
  |
2 |     let mut x = 5;
  |         +++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0384`.

Compound assignment through a shared reference is E0594, "cannot assign to *r, which is behind a & reference" - the transcript below is the program's full output, including the unused-mutability warning the binding earns:

Rust
fn main() {
    let mut x = 5;
    let r = &x;
    *r += 1;
    println!("{}", x);
}
Output
warning: variable does not need to be mutable
 --> n6.rs:2:9
  |
2 |     let mut x = 5;
  |         ----^
  |         |
  |         help: remove this `mut`
  |
  = note: `#[warn(unused_mut)]` (part of `#[warn(unused)]`) on by default

error[E0594]: cannot assign to `*r`, which is behind a `&` reference
 --> n6.rs:4:5
  |
4 |     *r += 1;
  |     ^^^^^^^ `r` is a `&` reference, so it cannot be written to
  |
help: consider changing this to be a mutable reference
  |
3 |     let r = &mut x;
  |              +++

error: aborting due to 1 previous error; 1 warning emitted

For more information about this error, try `rustc --explain E0594`.

And compound assignment to a binding that was never initialized is E0381 - again the full output, warning included:

Rust
fn main() {
    let mut x: i32;
    x += 1;
    println!("{}", x);
}
Output
warning: variable does not need to be mutable
 --> n5.rs:2:9
  |
2 |     let mut x: i32;
  |         ----^
  |         |
  |         help: remove this `mut`
  |
  = note: `#[warn(unused_mut)]` (part of `#[warn(unused)]`) on by default

error[E0381]: used binding `x` isn't initialized
 --> n5.rs:3:5
  |
2 |     let mut x: i32;
  |         ----- binding declared here but left uninitialized
3 |     x += 1;
  |     ^^^^^^ `x` used here but it isn't initialized
  |
help: consider assigning a value
  |
2 |     let mut x: i32 = 42;
  |                    ++++

error: aborting due to 1 previous error; 1 warning emitted

For more information about this error, try `rustc --explain E0381`.

That last one is structural, not a formality: x += 1 has to read the old value of x to add to it, and there is no old value. No trait implementation touches any of these three - the fixes are mut, &mut, and an initial value, in that order.

Fixes in reach order

First, for the index's own example shape, use the type the operation is defined for. The f32 shift, with the value as u32 exactly as the index's fixed example does, compiles and runs:

Rust
fn main() {
    let mut x = 12u32;
    x <<= 2;
    println!("{}", x);
}
Output
48

The output is 48, which is 12 << 2. This is the repair whenever the left-hand type is a built-in or standard-library type with no such operation - f32 shifting, bool with +=, a Vec with a single element. For the Vec shape, the working form is the type's own method rather than an operator at all - extend appends the element, and this program compiles and runs:

Rust
fn main() {
    let mut v = vec![1, 2];
    v.extend([3]);
    println!("{:?}", v);
}
Config
[1, 2, 3]

The output is [1, 2, 3] - the two original elements with 3 appended.

Second, for your own type, implement the assignment trait the diagnostic names. The custom-type failure, with AddAssign implemented for Foo, compiles and runs - note the implementation's shape, a mutable borrow of self and no return value, unlike Add:

Rust
use std::ops::AddAssign;
struct Foo(u32);
impl AddAssign for Foo {
    fn add_assign(&mut self, rhs: Foo) {
        self.0 += rhs.0;
    }
}
fn main() {
    let mut x = Foo(5);
    x += Foo(7);
    println!("{}", x.0);
}
Output
12

The output is 12, the sum of 5 and 7. If the right-hand side at your call site is a reference, implement the reference form the note names instead - AddAssign<&Foo> compiles and runs for the &Foo call that failed above:

Rust
use std::ops::AddAssign;
struct Foo(u32);
impl AddAssign<&Foo> for Foo {
    fn add_assign(&mut self, rhs: &Foo) {
        self.0 += rhs.0;
    }
}
fn main() {
    let mut x = Foo(5);
    let y = Foo(7);
    x += &y;
    println!("{}", x.0);
}
Output
12

The output is again 12. The two implementations are separate in the probed set - this one covers the &Foo call, the previous one the owned Foo call - so implement the form, or forms, your call sites use.

Third, when you already have the plain operator and do not need the compound form, rewrite with it. The Add-only type that failed with += runs unchanged through a plain assignment:

Rust
use std::ops::Add;
struct Foo(u32);
impl Add for Foo {
    type Output = Foo;
    fn add(self, rhs: Foo) -> Foo {
        Foo(self.0 + rhs.0)
    }
}
fn main() {
    let mut x = Foo(5);
    x = x + Foo(7);
    println!("{}", x.0);
}
Output
12

The output is 12. This repair costs a move of the left-hand value where AddAssign would have mutated in place, so it fits when the type is cheap to move or the compound form is not worth a second trait implementation; when the type is state you want mutated in place, the second fix is the real one.

Fourth, match the right-hand type to an implementation that exists. String with a &str on the right - the one AddAssign form the documentation lists for it - compiles and runs:

Rust
fn main() {
    let mut s = String::from("a");
    s += "b";
    println!("{}", s);
}
Command
ab

The output is ab. And matched primitive types run as written - an i32 with an i32, a f64 with a f64:

Rust
fn main() {
    let mut x: i32 = 5;
    x += 2;
    let mut y: f64 = 1.5;
    y += 2.5;
    println!("{} {}", x, y);
}
Output
7 4

The output is 7 4 - 5 + 2 and 1.5 + 2.5. For the E0277 and E0308 shapes, this is the whole repair: convert or retype the right-hand value until it names an implementation that exists, rather than reaching for a trait you cannot implement for someone else's types.

Last, through a mutable reference, the compound assignment works as written once the reference is &mut - the E0594 program with that one change compiles and runs:

Rust
fn main() {
    let mut x = 5;
    let r = &mut x;
    *r += 1;
    println!("{}", x);
}
Output
6

The output is 6, which is 5 + 1. And if the diagnostic is not E0368 at all, switch playbooks: E0369 wants the plain trait (see the E0369 guide), E0277 and E0308 want the right-hand type matched or converted, and E0384, E0594, and E0381 want mut, &mut, and an initial value respectively.

Decision summary

E0368, "binary assignment operation cannot be applied to type", is one shape in this guide's probed set: a compound assignment whose left-hand type has no assignment-trait implementation for that operator in play - a f32 with <<= or ^=, a bool with +=, a Vec with a single element, and a custom Foo with +=, whether Foo implements Add or no operator trait at all, and whether the right-hand side is an owned Foo or a &Foo, the note naming AddAssign or AddAssign<&Foo> accordingly. The diagnostic names the operator and the left-hand type in its header, underlines the whole compound expression, and for a custom type adds the missing-implementation notes. The repairs act on the implementation or the types: use a type the operation is defined for when the left-hand type is not yours, implement the named assignment trait - in the right-hand form your call sites use - when it is, rewrite with the plain operator when that trait already exists, or match the right-hand type to an existing implementation. The neighbors are distinct in the probed set: the plain operator on the same unsupported pair is E0369, a cross-type numeric compound assignment on a type that implements the assignment trait for other right-hand types is E0277, a String compound assignment with a non-&str right-hand side is E0308, and compound assignment to an immutable binding, through a shared reference, or to an uninitialized binding is E0384, E0594, or E0381.

Checklist

  • Read the code first: in the probed set, E0368 is the compound form with no assignment implementation for the operator, E0369 is the plain operator, E0277 is the cross-type numeric compound assignment, and E0308 is the String compound assignment with a non-&str right-hand side.
  • Read the missing-implementation note literally - in the probed set it names the exact form needed, AddAssign for an owned right-hand side and AddAssign<&Foo> for a reference one.
  • Do not expect Add to cover += - in the probed set, a type with only Add emitted E0368 for += and ran through x = x + y instead.
  • If the left-hand type is a built-in or standard-library type, do not try to implement the trait for it - in the probed set those shapes are repaired by changing the type, the operation, or the method used.
  • Match the right-hand type before reaching for a trait - in the probed set, String += &str and same-type numeric compound assignments compile as written.
  • Check the place before the operation: in the probed set, an immutable binding is E0384, a shared reference is E0594, and an uninitialized binding is E0381, and none of them is a trait problem.

Sources

  • Rust error codes index, E0368 - a binary assignment operator like += or ^= was applied to a type that doesn't support it; the erroneous example with 12f32 shifted by <<=; the fix example switching to u32, which implements the ShlAssign trait; the note that most operators can be overloaded by implementing the [OP]Assign traits from std::ops; the Foo example implementing Add but failing with +=, because AddAssign is not automatically implemented and must be implemented manually.
  • Rust error codes index, E0369 - a binary operation was attempted on a type which doesn't support it; the erroneous example with 12f32 shifted by the plain <<; the fix example switching to u32.
  • The Rust Reference, Operator expressions - compound assignment expressions combine arithmetic and logical binary operators with assignment expressions; the syntax of a mutable place expression, the assigned operand, then an operator followed by = as a single token, then a value expression, the modifying operand; outside the primitive case the expression is syntactic sugar for using the corresponding trait and calling its method with the left-hand side as the receiver; the operator table pairing + with Add and AddAssign, and << with Shl and ShlAssign.
  • std::ops::AddAssign - the trait behind +=; its implementations for the primitive numeric types, and impl AddAssign<&str> for String, which implements the += operator for appending to a String with the same behavior as the push_str method.

Keep Exploring