]>
Commit | Line | Data |
---|---|---|
60c5eb7d XL |
1 | Something other than numbers and characters has been used for a range. |
2 | ||
3 | Erroneous code example: | |
4 | ||
5 | ```compile_fail,E0029 | |
6 | let string = "salutations !"; | |
7 | ||
8 | // The ordering relation for strings cannot be evaluated at compile time, | |
9 | // so this doesn't work: | |
10 | match string { | |
11 | "hello" ..= "world" => {} | |
12 | _ => {} | |
13 | } | |
14 | ||
15 | // This is a more general version, using a guard: | |
16 | match string { | |
17 | s if s >= "hello" && s <= "world" => {} | |
18 | _ => {} | |
19 | } | |
20 | ``` | |
21 | ||
22 | In a match expression, only numbers and characters can be matched against a | |
23 | range. This is because the compiler checks that the range is non-empty at | |
24 | compile-time, and is unable to evaluate arbitrary comparison functions. If you | |
25 | want to capture values of an orderable type between two end-points, you can use | |
26 | a guard. |