]> git.lizzy.rs Git - rust.git/blob - src/doc/unstable-book/src/language-features/advanced-slice-patterns.md
Fix regression on `include!(line!())`.
[rust.git] / src / doc / unstable-book / src / language-features / advanced-slice-patterns.md
1 # `advanced_slice_patterns`
2
3 The tracking issue for this feature is: [#23121]
4
5 [#23121]: https://github.com/rust-lang/rust/issues/23121
6
7 See also [`slice_patterns`](slice-patterns.html).
8
9 ------------------------
10
11
12 The `advanced_slice_patterns` gate lets you use `..` to indicate any number of
13 elements inside a pattern matching a slice. This wildcard can only be used once
14 for a given array. If there's an identifier before the `..`, the result of the
15 slice will be bound to that name. For example:
16
17 ```rust
18 #![feature(advanced_slice_patterns, slice_patterns)]
19
20 fn is_symmetric(list: &[u32]) -> bool {
21     match list {
22         &[] | &[_] => true,
23         &[x, ref inside.., y] if x == y => is_symmetric(inside),
24         _ => false
25     }
26 }
27
28 fn main() {
29     let sym = &[0, 1, 4, 2, 4, 1, 0];
30     assert!(is_symmetric(sym));
31
32     let not_sym = &[0, 1, 7, 2, 4, 1, 0];
33     assert!(!is_symmetric(not_sym));
34 }
35 ```