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