@@ -4,25 +4,29 @@ The tracking issue for this feature is: [#23121]
4
4
5
5
[ #23121 ] : https://github.com/rust-lang/rust/issues/23121
6
6
7
- See also
8
- [ ` advanced_slice_patterns ` ] ( language-features/advanced-slice-patterns.html ) .
9
-
10
7
------------------------
11
8
12
-
13
- If you want to match against a slice or array, you can use ` & ` with the
14
- ` slice_patterns ` feature:
9
+ The ` slice_patterns ` feature gate lets you use ` .. ` to indicate any number of
10
+ elements inside a pattern matching a slice. This wildcard can only be used once
11
+ for a given array. If there's an pattern before the ` .. ` , the subslice will be
12
+ matched against that pattern. For example:
15
13
16
14
``` rust
17
15
#![feature(slice_patterns)]
18
16
17
+ fn is_symmetric (list : & [u32 ]) -> bool {
18
+ match list {
19
+ & [] | & [_ ] => true ,
20
+ & [x , ref inside .. , y ] if x == y => is_symmetric (inside ),
21
+ & [.. ] => false ,
22
+ }
23
+ }
24
+
19
25
fn main () {
20
- let v = vec! [" match_this" , " 1" ];
26
+ let sym = & [0 , 1 , 4 , 2 , 4 , 1 , 0 ];
27
+ assert! (is_symmetric (sym ));
21
28
22
- match & v [.. ] {
23
- & [" match_this" , second ] => println! (" The second element is {}" , second ),
24
- _ => {},
25
- }
29
+ let not_sym = & [0 , 1 , 7 , 2 , 4 , 1 , 0 ];
30
+ assert! (! is_symmetric (not_sym ));
26
31
}
27
32
```
28
-
0 commit comments