]> git.lizzy.rs Git - rust.git/blob - src/librustc_error_codes/error_codes/E0368.md
0bb283258c463b769b01b36bf7a1a69e8f3ab985
[rust.git] / src / librustc_error_codes / error_codes / E0368.md
1 This error indicates that a binary assignment operator like `+=` or `^=` was
2 applied to a type that doesn't support it. For example:
3
4 ```compile_fail,E0368
5 let mut x = 12f32; // error: binary operation `<<` cannot be applied to
6                    //        type `f32`
7
8 x <<= 2;
9 ```
10
11 To fix this error, please check that this type implements this binary
12 operation. Example:
13
14 ```
15 let mut x = 12u32; // the `u32` type does implement the `ShlAssign` trait
16
17 x <<= 2; // ok!
18 ```
19
20 It is also possible to overload most operators for your own type by
21 implementing the `[OP]Assign` traits from `std::ops`.
22
23 Another problem you might be facing is this: suppose you've overloaded the `+`
24 operator for some type `Foo` by implementing the `std::ops::Add` trait for
25 `Foo`, but you find that using `+=` does not work, as in this example:
26
27 ```compile_fail,E0368
28 use std::ops::Add;
29
30 struct Foo(u32);
31
32 impl Add for Foo {
33     type Output = Foo;
34
35     fn add(self, rhs: Foo) -> Foo {
36         Foo(self.0 + rhs.0)
37     }
38 }
39
40 fn main() {
41     let mut x: Foo = Foo(5);
42     x += Foo(7); // error, `+= cannot be applied to the type `Foo`
43 }
44 ```
45
46 This is because `AddAssign` is not automatically implemented, so you need to
47 manually implement it for your type.