]>
Commit | Line | Data |
---|---|---|
60c5eb7d XL |
1 | The `self` keyword was used inside of an associated function without a "`self` |
2 | receiver" parameter. | |
3 | ||
4 | Erroneous code example: | |
5 | ||
6 | ```compile_fail,E0424 | |
7 | struct Foo; | |
8 | ||
9 | impl Foo { | |
10 | // `bar` is a method, because it has a receiver parameter. | |
11 | fn bar(&self) {} | |
12 | ||
13 | // `foo` is not a method, because it has no receiver parameter. | |
14 | fn foo() { | |
15 | self.bar(); // error: `self` value is a keyword only available in | |
16 | // methods with a `self` parameter | |
17 | } | |
18 | } | |
19 | ``` | |
20 | ||
21 | The `self` keyword can only be used inside methods, which are associated | |
22 | functions (functions defined inside of a `trait` or `impl` block) that have a | |
23 | `self` receiver as its first parameter, like `self`, `&self`, `&mut self` or | |
24 | `self: &mut Pin<Self>` (this last one is an example of an ["abitrary `self` | |
25 | type"](https://github.com/rust-lang/rust/issues/44874)). | |
26 | ||
27 | Check if the associated function's parameter list should have contained a `self` | |
28 | receiver for it to be a method, and add it if so. Example: | |
29 | ||
30 | ``` | |
31 | struct Foo; | |
32 | ||
33 | impl Foo { | |
34 | fn bar(&self) {} | |
35 | ||
36 | fn foo(self) { // `foo` is now a method. | |
37 | self.bar(); // ok! | |
38 | } | |
39 | } | |
40 | ``` |