]> git.lizzy.rs Git - rust.git/blob - src/librustc_error_codes/error_codes/E0009.md
Rollup merge of #67875 - dtolnay:hidden, r=GuillaumeGomez
[rust.git] / src / librustc_error_codes / error_codes / E0009.md
1 In a pattern, all values that don't implement the `Copy` trait have to be bound
2 the same way. The goal here is to avoid binding simultaneously by-move and
3 by-ref.
4
5 This limitation may be removed in a future version of Rust.
6
7 Erroneous code example:
8
9 ```compile_fail,E0009
10 struct X { x: (), }
11
12 let x = Some((X { x: () }, X { x: () }));
13 match x {
14     Some((y, ref z)) => {}, // error: cannot bind by-move and by-ref in the
15                             //        same pattern
16     None => panic!()
17 }
18 ```
19
20 You have two solutions:
21
22 Solution #1: Bind the pattern's values the same way.
23
24 ```
25 struct X { x: (), }
26
27 let x = Some((X { x: () }, X { x: () }));
28 match x {
29     Some((ref y, ref z)) => {},
30     // or Some((y, z)) => {}
31     None => panic!()
32 }
33 ```
34
35 Solution #2: Implement the `Copy` trait for the `X` structure.
36
37 However, please keep in mind that the first solution should be preferred.
38
39 ```
40 #[derive(Clone, Copy)]
41 struct X { x: (), }
42
43 let x = Some((X { x: () }, X { x: () }));
44 match x {
45     Some((y, ref z)) => {},
46     None => panic!()
47 }
48 ```