]>
Commit | Line | Data |
---|---|---|
60c5eb7d XL |
1 | An array without a fixed length was pattern-matched. |
2 | ||
3dfed10e | 3 | Erroneous code example: |
60c5eb7d XL |
4 | |
5 | ```compile_fail,E0730 | |
6 | #![feature(const_generics)] | |
7 | ||
8 | fn is_123<const N: usize>(x: [u32; N]) -> bool { | |
9 | match x { | |
ba9703b0 XL |
10 | [1, 2, ..] => true, // error: cannot pattern-match on an |
11 | // array without a fixed length | |
60c5eb7d XL |
12 | _ => false |
13 | } | |
14 | } | |
15 | ``` | |
16 | ||
3dfed10e XL |
17 | To fix this error, you have two solutions: |
18 | 1. Use an array with a fixed length. | |
19 | 2. Use a slice. | |
60c5eb7d | 20 | |
3dfed10e XL |
21 | Example with an array with a fixed length: |
22 | ||
23 | ``` | |
24 | fn is_123(x: [u32; 3]) -> bool { // We use an array with a fixed size | |
25 | match x { | |
26 | [1, 2, ..] => true, // ok! | |
27 | _ => false | |
28 | } | |
29 | } | |
60c5eb7d | 30 | ``` |
3dfed10e XL |
31 | |
32 | Example with a slice: | |
33 | ||
34 | ``` | |
35 | fn is_123(x: &[u32]) -> bool { // We use a slice | |
36 | match x { | |
37 | [1, 2, ..] => true, // ok! | |
38 | _ => false | |
60c5eb7d XL |
39 | } |
40 | } | |
41 | ``` |