]> git.lizzy.rs Git - rust.git/blob - src/doc/unstable-book/src/language-features/slice-patterns.md
Rollup merge of #63055 - Mark-Simulacrum:save-analysis-clean-2, r=Xanewok
[rust.git] / src / doc / unstable-book / src / language-features / slice-patterns.md
1 # `slice_patterns`
2
3 The tracking issue for this feature is: [#62254]
4
5 [#62254]: https://github.com/rust-lang/rust/issues/62254
6
7 ------------------------
8
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:
13
14 ```rust
15 #![feature(slice_patterns)]
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
25 fn main() {
26     let sym = &[0, 1, 4, 2, 4, 1, 0];
27     assert!(is_symmetric(sym));
28
29     let not_sym = &[0, 1, 7, 2, 4, 1, 0];
30     assert!(!is_symmetric(not_sym));
31 }
32 ```