]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_error_codes/src/error_codes/E0158.md
New upstream version 1.48.0~beta.8+dfsg1
[rustc.git] / compiler / rustc_error_codes / src / error_codes / E0158.md
1 An associated const has been referenced in a pattern.
2
3 Erroneous code example:
4
5 ```compile_fail,E0158
6 enum EFoo { A, B, C, D }
7
8 trait Foo {
9 const X: EFoo;
10 }
11
12 fn test<A: Foo>(arg: EFoo) {
13 match arg {
14 A::X => { // error!
15 println!("A::X");
16 }
17 }
18 }
19 ```
20
21 `const` and `static` mean different things. A `const` is a compile-time
22 constant, an alias for a literal value. This property means you can match it
23 directly within a pattern.
24
25 The `static` keyword, on the other hand, guarantees a fixed location in memory.
26 This does not always mean that the value is constant. For example, a global
27 mutex can be declared `static` as well.
28
29 If you want to match against a `static`, consider using a guard instead:
30
31 ```
32 static FORTY_TWO: i32 = 42;
33
34 match Some(42) {
35 Some(x) if x == FORTY_TWO => {}
36 _ => {}
37 }
38 ```