]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0573.md
Rollup merge of #93556 - dtolnay:trailingcomma, r=cjgillot
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0573.md
1 Something other than a type has been used when one was expected.
2
3 Erroneous code examples:
4
5 ```compile_fail,E0573
6 enum Dragon {
7     Born,
8 }
9
10 fn oblivion() -> Dragon::Born { // error!
11     Dragon::Born
12 }
13
14 const HOBBIT: u32 = 2;
15 impl HOBBIT {} // error!
16
17 enum Wizard {
18     Gandalf,
19     Saruman,
20 }
21
22 trait Isengard {
23     fn wizard(_: Wizard::Saruman); // error!
24 }
25 ```
26
27 In all these errors, a type was expected. For example, in the first error, if
28 we want to return the `Born` variant from the `Dragon` enum, we must set the
29 function to return the enum and not its variant:
30
31 ```
32 enum Dragon {
33     Born,
34 }
35
36 fn oblivion() -> Dragon { // ok!
37     Dragon::Born
38 }
39 ```
40
41 In the second error, you can't implement something on an item, only on types.
42 We would need to create a new type if we wanted to do something similar:
43
44 ```
45 struct Hobbit(u32); // we create a new type
46
47 const HOBBIT: Hobbit = Hobbit(2);
48 impl Hobbit {} // ok!
49 ```
50
51 In the third case, we tried to only expect one variant of the `Wizard` enum,
52 which is not possible. To make this work, we need to using pattern matching
53 over the `Wizard` enum:
54
55 ```
56 enum Wizard {
57     Gandalf,
58     Saruman,
59 }
60
61 trait Isengard {
62     fn wizard(w: Wizard) { // ok!
63         match w {
64             Wizard::Saruman => {
65                 // do something
66             }
67             _ => {} // ignore everything else
68         }
69     }
70 }
71 ```