@@ -4,25 +4,29 @@ The tracking issue for this feature is: [#23121]
44
55[ #23121 ] : https://github.com/rust-lang/rust/issues/23121
66
7- See also
8- [ ` advanced_slice_patterns ` ] ( language-features/advanced-slice-patterns.html ) .
9-
107------------------------
118
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:
1513
1614``` rust
1715#![feature(slice_patterns)]
1816
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+
1925fn main () {
20- let v = vec! [" match_this" , " 1" ];
26+ let sym = & [0 , 1 , 4 , 2 , 4 , 1 , 0 ];
27+ assert! (is_symmetric (sym ));
2128
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 ));
2631}
2732```
28-
0 commit comments