]>
Commit | Line | Data |
---|---|---|
60c5eb7d XL |
1 | A private item was used outside of its scope. |
2 | ||
3 | Erroneous code example: | |
4 | ||
5 | ```compile_fail,E0624 | |
6 | mod inner { | |
7 | pub struct Foo; | |
8 | ||
9 | impl Foo { | |
10 | fn method(&self) {} | |
11 | } | |
12 | } | |
13 | ||
14 | let foo = inner::Foo; | |
15 | foo.method(); // error: method `method` is private | |
16 | ``` | |
17 | ||
18 | Two possibilities are available to solve this issue: | |
19 | ||
20 | 1. Only use the item in the scope it has been defined: | |
21 | ||
22 | ``` | |
23 | mod inner { | |
24 | pub struct Foo; | |
25 | ||
26 | impl Foo { | |
27 | fn method(&self) {} | |
28 | } | |
29 | ||
30 | pub fn call_method(foo: &Foo) { // We create a public function. | |
31 | foo.method(); // Which calls the item. | |
32 | } | |
33 | } | |
34 | ||
35 | let foo = inner::Foo; | |
36 | inner::call_method(&foo); // And since the function is public, we can call the | |
37 | // method through it. | |
38 | ``` | |
39 | ||
40 | 2. Make the item public: | |
41 | ||
42 | ``` | |
43 | mod inner { | |
44 | pub struct Foo; | |
45 | ||
46 | impl Foo { | |
47 | pub fn method(&self) {} // It's now public. | |
48 | } | |
49 | } | |
50 | ||
51 | let foo = inner::Foo; | |
52 | foo.method(); // Ok! | |
53 | ``` |