Skip to content

Commit b577bee

Browse files
committed
copyedits: patterns
This also puts slice patterns in nightly docs, where they belong.
1 parent 9aa4b64 commit b577bee

File tree

3 files changed

+66
-51
lines changed

3 files changed

+66
-51
lines changed

src/doc/trpl/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,5 @@
6767
* [Link args](link-args.md)
6868
* [Benchmark Tests](benchmark-tests.md)
6969
* [Box Syntax and Patterns](box-syntax-and-patterns.md)
70+
* [Slice Patterns](slice-patterns.md)
7071
* [Glossary](glossary.md)

src/doc/trpl/patterns.md

Lines changed: 47 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
% Patterns
22

3-
We've made use of patterns a few times in the guide: first with `let` bindings,
4-
then with `match` statements. Let's go on a whirlwind tour of all of the things
5-
patterns can do!
3+
Patterns are quite common in Rust. We use them in [variable
4+
bindings][bindings], [match statements][match], and other places, too. Let’s go
5+
on a whirlwind tour of all of the things patterns can do!
6+
7+
[bindings]: variable-bindings.html
8+
[match]: match.html
69

710
A quick refresher: you can match against literals directly, and `_` acts as an
8-
*any* case:
11+
any case:
912

10-
```{rust}
13+
```rust
1114
let x = 1;
1215

1316
match x {
@@ -18,9 +21,11 @@ match x {
1821
}
1922
```
2023

24+
# Multiple patterns
25+
2126
You can match multiple patterns with `|`:
2227

23-
```{rust}
28+
```rust
2429
let x = 1;
2530

2631
match x {
@@ -30,9 +35,11 @@ match x {
3035
}
3136
```
3237

38+
# Ranges
39+
3340
You can match a range of values with `...`:
3441

35-
```{rust}
42+
```rust
3643
let x = 1;
3744

3845
match x {
@@ -43,10 +50,12 @@ match x {
4350

4451
Ranges are mostly used with integers and single characters.
4552

46-
If you're matching multiple things, via a `|` or a `...`, you can bind
53+
# Bindings
54+
55+
If you’re matching multiple things, via a `|` or a `...`, you can bind
4756
the value to a name with `@`:
4857

49-
```{rust}
58+
```rust
5059
let x = 1;
5160

5261
match x {
@@ -55,10 +64,12 @@ match x {
5564
}
5665
```
5766

58-
If you're matching on an enum which has variants, you can use `..` to
67+
# Ignoring variants
68+
69+
If you’re matching on an enum which has variants, you can use `..` to
5970
ignore the value and type in the variant:
6071

61-
```{rust}
72+
```rust
6273
enum OptionalInt {
6374
Value(i32),
6475
Missing,
@@ -72,9 +83,11 @@ match x {
7283
}
7384
```
7485

75-
You can introduce *match guards* with `if`:
86+
# Guards
87+
88+
You can introduce ‘match guards’ with `if`:
7689

77-
```{rust}
90+
```rust
7891
enum OptionalInt {
7992
Value(i32),
8093
Missing,
@@ -89,47 +102,38 @@ match x {
89102
}
90103
```
91104

92-
If you're matching on a pointer, you can use the same syntax as you declared it
93-
with. First, `&`:
94-
95-
```{rust}
96-
let x = &5;
97-
98-
match x {
99-
&val => println!("Got a value: {}", val),
100-
}
101-
```
102-
103-
Here, the `val` inside the `match` has type `i32`. In other words, the left-hand
104-
side of the pattern destructures the value. If we have `&5`, then in `&val`, `val`
105-
would be `5`.
105+
# ref and ref mut
106106

107-
If you want to get a reference, use the `ref` keyword:
107+
If you want to get a [reference][ref], use the `ref` keyword:
108108

109-
```{rust}
109+
```rust
110110
let x = 5;
111111

112112
match x {
113113
ref r => println!("Got a reference to {}", r),
114114
}
115115
```
116116

117+
[ref]: references-and-borrowing.html
118+
117119
Here, the `r` inside the `match` has the type `&i32`. In other words, the `ref`
118120
keyword _creates_ a reference, for use in the pattern. If you need a mutable
119121
reference, `ref mut` will work in the same way:
120122

121-
```{rust}
123+
```rust
122124
let mut x = 5;
123125

124126
match x {
125127
ref mut mr => println!("Got a mutable reference to {}", mr),
126128
}
127129
```
128130

129-
If you have a struct, you can destructure it inside of a pattern:
131+
# Destructuring
132+
133+
If you have a compound data type, like a `struct`, you can destructure it
134+
inside of a pattern:
130135

131-
```{rust}
132-
# #![allow(non_shorthand_field_patterns)]
136+
```rust
133137
struct Point {
134138
x: i32,
135139
y: i32,
@@ -142,10 +146,9 @@ match origin {
142146
}
143147
```
144148

145-
If we only care about some of the values, we don't have to give them all names:
149+
If we only care about some of the values, we dont have to give them all names:
146150

147-
```{rust}
148-
# #![allow(non_shorthand_field_patterns)]
151+
```rust
149152
struct Point {
150153
x: i32,
151154
y: i32,
@@ -160,8 +163,7 @@ match origin {
160163

161164
You can do this kind of match on any member, not just the first:
162165

163-
```{rust}
164-
# #![allow(non_shorthand_field_patterns)]
166+
```rust
165167
struct Point {
166168
x: i32,
167169
y: i32,
@@ -174,22 +176,16 @@ match origin {
174176
}
175177
```
176178

177-
If you want to match against a slice or array, you can use `&`:
179+
This ‘destructuring’ behavior works on any compound data type, like
180+
[tuples][tuples] or [enums][enums].
178181

179-
```{rust}
180-
# #![feature(slice_patterns)]
181-
fn main() {
182-
let v = vec!["match_this", "1"];
182+
[tuples]: primitive-types.html#tuples
183+
[enums]: enums.html
183184

184-
match &v[..] {
185-
["match_this", second] => println!("The second element is {}", second),
186-
_ => {},
187-
}
188-
}
189-
```
185+
# Mix and Match
190186

191-
Whew! That's a lot of different ways to match things, and they can all be
192-
mixed and matched, depending on what you're doing:
187+
Whew! Thats a lot of different ways to match things, and they can all be
188+
mixed and matched, depending on what youre doing:
193189

194190
```{rust,ignore}
195191
match x {

src/doc/trpl/slice-patterns.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
% Slice patterns
2+
3+
If you want to match against a slice or array, you can use `&` with the
4+
`slice_patterns` feature:
5+
6+
```rust
7+
#![feature(slice_patterns)]
8+
9+
fn main() {
10+
let v = vec!["match_this", "1"];
11+
12+
match &v[..] {
13+
["match_this", second] => println!("The second element is {}", second),
14+
_ => {},
15+
}
16+
}
17+
```
18+

0 commit comments

Comments
 (0)