]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_error_codes/src/error_codes/E0034.md
New upstream version 1.48.0~beta.8+dfsg1
[rustc.git] / compiler / rustc_error_codes / src / error_codes / E0034.md
1 The compiler doesn't know what method to call because more than one method
2 has the same prototype.
3
4 Erroneous code example:
5
6 ```compile_fail,E0034
7 struct Test;
8
9 trait Trait1 {
10 fn foo();
11 }
12
13 trait Trait2 {
14 fn foo();
15 }
16
17 impl Trait1 for Test { fn foo() {} }
18 impl Trait2 for Test { fn foo() {} }
19
20 fn main() {
21 Test::foo() // error, which foo() to call?
22 }
23 ```
24
25 To avoid this error, you have to keep only one of them and remove the others.
26 So let's take our example and fix it:
27
28 ```
29 struct Test;
30
31 trait Trait1 {
32 fn foo();
33 }
34
35 impl Trait1 for Test { fn foo() {} }
36
37 fn main() {
38 Test::foo() // and now that's good!
39 }
40 ```
41
42 However, a better solution would be using fully explicit naming of type and
43 trait:
44
45 ```
46 struct Test;
47
48 trait Trait1 {
49 fn foo();
50 }
51
52 trait Trait2 {
53 fn foo();
54 }
55
56 impl Trait1 for Test { fn foo() {} }
57 impl Trait2 for Test { fn foo() {} }
58
59 fn main() {
60 <Test as Trait1>::foo()
61 }
62 ```
63
64 One last example:
65
66 ```
67 trait F {
68 fn m(&self);
69 }
70
71 trait G {
72 fn m(&self);
73 }
74
75 struct X;
76
77 impl F for X { fn m(&self) { println!("I am F"); } }
78 impl G for X { fn m(&self) { println!("I am G"); } }
79
80 fn main() {
81 let f = X;
82
83 F::m(&f); // it displays "I am F"
84 G::m(&f); // it displays "I am G"
85 }
86 ```