]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0170.md
Rollup merge of #93556 - dtolnay:trailingcomma, r=cjgillot
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0170.md
1 A pattern binding is using the same name as one of the variants of a type.
2
3 Erroneous code example:
4
5 ```compile_fail,E0170
6 # #![deny(warnings)]
7 enum Method {
8     GET,
9     POST,
10 }
11
12 fn is_empty(s: Method) -> bool {
13     match s {
14         GET => true,
15         _ => false
16     }
17 }
18
19 fn main() {}
20 ```
21
22 Enum variants are qualified by default. For example, given this type:
23
24 ```
25 enum Method {
26     GET,
27     POST,
28 }
29 ```
30
31 You would match it using:
32
33 ```
34 enum Method {
35     GET,
36     POST,
37 }
38
39 let m = Method::GET;
40
41 match m {
42     Method::GET => {},
43     Method::POST => {},
44 }
45 ```
46
47 If you don't qualify the names, the code will bind new variables named "GET" and
48 "POST" instead. This behavior is likely not what you want, so `rustc` warns when
49 that happens.
50
51 Qualified names are good practice, and most code works well with them. But if
52 you prefer them unqualified, you can import the variants into scope:
53
54 ```
55 use Method::*;
56 enum Method { GET, POST }
57 # fn main() {}
58 ```
59
60 If you want others to be able to import variants from your module directly, use
61 `pub use`:
62
63 ```
64 pub use Method::*;
65 pub enum Method { GET, POST }
66 # fn main() {}
67 ```