]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0283.md
Rollup merge of #93556 - dtolnay:trailingcomma, r=cjgillot
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0283.md
1 An implementation cannot be chosen unambiguously because of lack of information.
2
3 Erroneous code example:
4
5 ```compile_fail,E0283
6 trait Generator {
7     fn create() -> u32;
8 }
9
10 struct Impl;
11
12 impl Generator for Impl {
13     fn create() -> u32 { 1 }
14 }
15
16 struct AnotherImpl;
17
18 impl Generator for AnotherImpl {
19     fn create() -> u32 { 2 }
20 }
21
22 fn main() {
23     let cont: u32 = Generator::create();
24     // error, impossible to choose one of Generator trait implementation
25     // Should it be Impl or AnotherImpl, maybe something else?
26 }
27 ```
28
29 This error can be solved by adding type annotations that provide the missing
30 information to the compiler. In this case, the solution is to use a concrete
31 type:
32
33 ```
34 trait Generator {
35     fn create() -> u32;
36 }
37
38 struct AnotherImpl;
39
40 impl Generator for AnotherImpl {
41     fn create() -> u32 { 2 }
42 }
43
44 fn main() {
45     let gen1 = AnotherImpl::create();
46
47     // if there are multiple methods with same name (different traits)
48     let gen2 = <AnotherImpl as Generator>::create();
49 }
50 ```