diff options
| author | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2018-02-24 22:21:33 +0300 |
|---|---|---|
| committer | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2018-03-20 02:27:40 +0300 |
| commit | 7c90189e1331cea3eac0ab0e8959f664cffba1ae (patch) | |
| tree | 839b20a700f22400b0be58ba059f84262de7c9c6 /src/doc | |
| parent | a04b88d1941644df01fa5e31dd43e0f57c13d938 (diff) | |
| download | rust-7c90189e1331cea3eac0ab0e8959f664cffba1ae.tar.gz rust-7c90189e1331cea3eac0ab0e8959f664cffba1ae.zip | |
Stabilize slice patterns without `..`
Merge `feature(advanced_slice_patterns)` into `feature(slice_patterns)`
Diffstat (limited to 'src/doc')
| -rw-r--r-- | src/doc/unstable-book/src/language-features/advanced-slice-patterns.md | 35 | ||||
| -rw-r--r-- | src/doc/unstable-book/src/language-features/slice-patterns.md | 28 |
2 files changed, 16 insertions, 47 deletions
diff --git a/src/doc/unstable-book/src/language-features/advanced-slice-patterns.md b/src/doc/unstable-book/src/language-features/advanced-slice-patterns.md deleted file mode 100644 index e8256469b14..00000000000 --- a/src/doc/unstable-book/src/language-features/advanced-slice-patterns.md +++ /dev/null @@ -1,35 +0,0 @@ -# `advanced_slice_patterns` - -The tracking issue for this feature is: [#23121] - -[#23121]: https://github.com/rust-lang/rust/issues/23121 - -See also [`slice_patterns`](language-features/slice-patterns.html). - ------------------------- - - -The `advanced_slice_patterns` gate lets you use `..` to indicate any number of -elements inside a pattern matching a slice. This wildcard can only be used once -for a given array. If there's an identifier before the `..`, the result of the -slice will be bound to that name. For example: - -```rust -#![feature(advanced_slice_patterns, slice_patterns)] - -fn is_symmetric(list: &[u32]) -> bool { - match list { - &[] | &[_] => true, - &[x, ref inside.., y] if x == y => is_symmetric(inside), - _ => false - } -} - -fn main() { - let sym = &[0, 1, 4, 2, 4, 1, 0]; - assert!(is_symmetric(sym)); - - let not_sym = &[0, 1, 7, 2, 4, 1, 0]; - assert!(!is_symmetric(not_sym)); -} -``` diff --git a/src/doc/unstable-book/src/language-features/slice-patterns.md b/src/doc/unstable-book/src/language-features/slice-patterns.md index 69857297582..133174268ef 100644 --- a/src/doc/unstable-book/src/language-features/slice-patterns.md +++ b/src/doc/unstable-book/src/language-features/slice-patterns.md @@ -4,25 +4,29 @@ The tracking issue for this feature is: [#23121] [#23121]: https://github.com/rust-lang/rust/issues/23121 -See also -[`advanced_slice_patterns`](language-features/advanced-slice-patterns.html). - ------------------------ - -If you want to match against a slice or array, you can use `&` with the -`slice_patterns` feature: +The `slice_patterns` feature gate lets you use `..` to indicate any number of +elements inside a pattern matching a slice. This wildcard can only be used once +for a given array. If there's an pattern before the `..`, the subslice will be +matched against that pattern. For example: ```rust #![feature(slice_patterns)] +fn is_symmetric(list: &[u32]) -> bool { + match list { + &[] | &[_] => true, + &[x, ref inside.., y] if x == y => is_symmetric(inside), + &[..] => false, + } +} + fn main() { - let v = vec!["match_this", "1"]; + let sym = &[0, 1, 4, 2, 4, 1, 0]; + assert!(is_symmetric(sym)); - match &v[..] { - &["match_this", second] => println!("The second element is {}", second), - _ => {}, - } + let not_sym = &[0, 1, 7, 2, 4, 1, 0]; + assert!(!is_symmetric(not_sym)); } ``` - |
