Skip to content

Commit c1f938d

Browse files
committed
Rearrange sections in "Patterns"
- Move "Destructuring" after "Multiple patterns", because some of later sections include examples which make use of destructuring. - Move "Ignoring bindings" after "Destructoring", because the former features Result<T,E> destructuring. Some of examples in later sections use "_" and "..", so "Ignoring bindings" must be positioned before them. - Fix #27347 by moving "Ref and mut ref" before "Ranges" and "Bindings", because "Bindings" section includes a somewhat difficult example, which also makes use of "ref" and "mut ref" operators.
1 parent 4b79add commit c1f938d

File tree

1 file changed

+122
-122
lines changed

1 file changed

+122
-122
lines changed

src/doc/trpl/patterns.md

Lines changed: 122 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -39,79 +39,81 @@ match x {
3939

4040
This prints `one or two`.
4141

42-
# Ranges
42+
# Destructuring
4343

44-
You can match a range of values with `...`:
44+
If you have a compound data type, like a [`struct`][struct], you can destructure it
45+
inside of a pattern:
4546

4647
```rust
47-
let x = 1;
48-
49-
match x {
50-
1 ... 5 => println!("one through five"),
51-
_ => println!("anything"),
48+
struct Point {
49+
x: i32,
50+
y: i32,
5251
}
53-
```
54-
55-
This prints `one through five`.
5652

57-
Ranges are mostly used with integers and `char`s:
58-
59-
```rust
60-
let x = '💅';
53+
let origin = Point { x: 0, y: 0 };
6154

62-
match x {
63-
'a' ... 'j' => println!("early letter"),
64-
'k' ... 'z' => println!("late letter"),
65-
_ => println!("something else"),
55+
match origin {
56+
Point { x, y } => println!("({},{})", x, y),
6657
}
6758
```
6859

69-
This prints `something else`.
70-
71-
# Bindings
60+
[struct]: structs.html
7261

73-
You can bind values to names with `@`:
62+
We can use `:` to give a value a different name.
7463

7564
```rust
76-
let x = 1;
65+
struct Point {
66+
x: i32,
67+
y: i32,
68+
}
7769

78-
match x {
79-
e @ 1 ... 5 => println!("got a range element {}", e),
80-
_ => println!("anything"),
70+
let origin = Point { x: 0, y: 0 };
71+
72+
match origin {
73+
Point { x: x1, y: y1 } => println!("({},{})", x1, y1),
8174
}
8275
```
8376

84-
This prints `got a range element 1`. This is useful when you want to
85-
do a complicated match of part of a data structure:
77+
If we only care about some of the values, we don’t have to give them all names:
8678

8779
```rust
88-
#[derive(Debug)]
89-
struct Person {
90-
name: Option<String>,
80+
struct Point {
81+
x: i32,
82+
y: i32,
9183
}
9284

93-
let name = "Steve".to_string();
94-
let mut x: Option<Person> = Some(Person { name: Some(name) });
95-
match x {
96-
Some(Person { name: ref a @ Some(_), .. }) => println!("{:?}", a),
97-
_ => {}
85+
let origin = Point { x: 0, y: 0 };
86+
87+
match origin {
88+
Point { x, .. } => println!("x is {}", x),
9889
}
9990
```
10091

101-
This prints `Some("Steve")`: We’ve bound the inner `name` to `a`.
92+
This prints `x is 0`.
10293

103-
If you use `@` with `|`, you need to make sure the name is bound in each part
104-
of the pattern:
94+
You can do this kind of match on any member, not just the first:
10595

10696
```rust
107-
let x = 5;
97+
struct Point {
98+
x: i32,
99+
y: i32,
100+
}
108101

109-
match x {
110-
e @ 1 ... 5 | e @ 8 ... 10 => println!("got a range element {}", e),
111-
_ => println!("anything"),
102+
let origin = Point { x: 0, y: 0 };
103+
104+
match origin {
105+
Point { y, .. } => println!("y is {}", y),
112106
}
113107
```
114108

109+
This prints `y is 0`.
110+
111+
This ‘destructuring’ behavior works on any compound data type, like
112+
[tuples][tuples] or [enums][enums].
113+
114+
[tuples]: primitive-types.html#tuples
115+
[enums]: enums.html
116+
115117
# Ignoring bindings
116118

117119
You can use `_` in a pattern to disregard the type and value.
@@ -162,154 +164,152 @@ match x {
162164

163165
This prints `Got a tuple!`.
164166

165-
# Guards
167+
# ref and ref mut
166168

167-
You can introduce ‘match guards’ with `if`:
169+
If you want to get a [reference][ref], use the `ref` keyword:
168170

169171
```rust
170-
enum OptionalInt {
171-
Value(i32),
172-
Missing,
173-
}
174-
175-
let x = OptionalInt::Value(5);
172+
let x = 5;
176173

177174
match x {
178-
OptionalInt::Value(i) if i > 5 => println!("Got an int bigger than five!"),
179-
OptionalInt::Value(..) => println!("Got an int!"),
180-
OptionalInt::Missing => println!("No such luck."),
175+
ref r => println!("Got a reference to {}", r),
181176
}
182177
```
183178

184-
This prints `Got an int!`.
179+
This prints `Got a reference to 5`.
185180

186-
If you’re using `if` with multiple patterns, the `if` applies to both sides:
181+
[ref]: references-and-borrowing.html
182+
183+
Here, the `r` inside the `match` has the type `&i32`. In other words, the `ref`
184+
keyword _creates_ a reference, for use in the pattern. If you need a mutable
185+
reference, `ref mut` will work in the same way:
187186

188187
```rust
189-
let x = 4;
190-
let y = false;
188+
let mut x = 5;
191189

192190
match x {
193-
4 | 5 if y => println!("yes"),
194-
_ => println!("no"),
191+
ref mut mr => println!("Got a mutable reference to {}", mr),
195192
}
196193
```
197194

198-
This prints `no`, because the `if` applies to the whole of `4 | 5`, and not to
199-
just the `5`, In other words, the the precedence of `if` behaves like this:
195+
# Ranges
200196

201-
```text
202-
(4 | 5) if y => ...
203-
```
197+
You can match a range of values with `...`:
204198

205-
not this:
199+
```rust
200+
let x = 1;
206201

207-
```text
208-
4 | (5 if y) => ...
202+
match x {
203+
1 ... 5 => println!("one through five"),
204+
_ => println!("anything"),
205+
}
209206
```
210207

211-
# ref and ref mut
208+
This prints `one through five`.
212209

213-
If you want to get a [reference][ref], use the `ref` keyword:
210+
Ranges are mostly used with integers and `char`s:
214211

215212
```rust
216-
let x = 5;
213+
let x = '💅';
217214

218215
match x {
219-
ref r => println!("Got a reference to {}", r),
216+
'a' ... 'j' => println!("early letter"),
217+
'k' ... 'z' => println!("late letter"),
218+
_ => println!("something else"),
220219
}
221220
```
222221

223-
This prints `Got a reference to 5`.
222+
This prints `something else`.
224223

225-
[ref]: references-and-borrowing.html
224+
# Bindings
226225

227-
Here, the `r` inside the `match` has the type `&i32`. In other words, the `ref`
228-
keyword _creates_ a reference, for use in the pattern. If you need a mutable
229-
reference, `ref mut` will work in the same way:
226+
You can bind values to names with `@`:
230227

231228
```rust
232-
let mut x = 5;
229+
let x = 1;
233230

234231
match x {
235-
ref mut mr => println!("Got a mutable reference to {}", mr),
232+
e @ 1 ... 5 => println!("got a range element {}", e),
233+
_ => println!("anything"),
236234
}
237235
```
238236

239-
# Destructuring
240-
241-
If you have a compound data type, like a [`struct`][struct], you can destructure it
242-
inside of a pattern:
237+
This prints `got a range element 1`. This is useful when you want to
238+
do a complicated match of part of a data structure:
243239

244240
```rust
245-
struct Point {
246-
x: i32,
247-
y: i32,
241+
#[derive(Debug)]
242+
struct Person {
243+
name: Option<String>,
248244
}
249245

250-
let origin = Point { x: 0, y: 0 };
251-
252-
match origin {
253-
Point { x, y } => println!("({},{})", x, y),
246+
let name = "Steve".to_string();
247+
let mut x: Option<Person> = Some(Person { name: Some(name) });
248+
match x {
249+
Some(Person { name: ref a @ Some(_), .. }) => println!("{:?}", a),
250+
_ => {}
254251
}
255252
```
256253

257-
[struct]: structs.html
254+
This prints `Some("Steve")`: We’ve bound the inner `name` to `a`.
258255

259-
We can use `:` to give a value a different name.
256+
If you use `@` with `|`, you need to make sure the name is bound in each part
257+
of the pattern:
260258

261259
```rust
262-
struct Point {
263-
x: i32,
264-
y: i32,
265-
}
266-
267-
let origin = Point { x: 0, y: 0 };
260+
let x = 5;
268261

269-
match origin {
270-
Point { x: x1, y: y1 } => println!("({},{})", x1, y1),
262+
match x {
263+
e @ 1 ... 5 | e @ 8 ... 10 => println!("got a range element {}", e),
264+
_ => println!("anything"),
271265
}
272266
```
273267

274-
If we only care about some of the values, we don’t have to give them all names:
268+
# Guards
269+
270+
You can introduce ‘match guards’ with `if`:
275271

276272
```rust
277-
struct Point {
278-
x: i32,
279-
y: i32,
273+
enum OptionalInt {
274+
Value(i32),
275+
Missing,
280276
}
281277

282-
let origin = Point { x: 0, y: 0 };
278+
let x = OptionalInt::Value(5);
283279

284-
match origin {
285-
Point { x, .. } => println!("x is {}", x),
280+
match x {
281+
OptionalInt::Value(i) if i > 5 => println!("Got an int bigger than five!"),
282+
OptionalInt::Value(..) => println!("Got an int!"),
283+
OptionalInt::Missing => println!("No such luck."),
286284
}
287285
```
288286

289-
This prints `x is 0`.
287+
This prints `Got an int!`.
290288

291-
You can do this kind of match on any member, not just the first:
289+
If you’re using `if` with multiple patterns, the `if` applies to both sides:
292290

293291
```rust
294-
struct Point {
295-
x: i32,
296-
y: i32,
297-
}
298-
299-
let origin = Point { x: 0, y: 0 };
292+
let x = 4;
293+
let y = false;
300294

301-
match origin {
302-
Point { y, .. } => println!("y is {}", y),
295+
match x {
296+
4 | 5 if y => println!("yes"),
297+
_ => println!("no"),
303298
}
304299
```
305300

306-
This prints `y is 0`.
301+
This prints `no`, because the `if` applies to the whole of `4 | 5`, and not to
302+
just the `5`, In other words, the the precedence of `if` behaves like this:
307303

308-
This ‘destructuring’ behavior works on any compound data type, like
309-
[tuples][tuples] or [enums][enums].
304+
```text
305+
(4 | 5) if y => ...
306+
```
310307

311-
[tuples]: primitive-types.html#tuples
312-
[enums]: enums.html
308+
not this:
309+
310+
```text
311+
4 | (5 if y) => ...
312+
```
313313

314314
# Mix and Match
315315

0 commit comments

Comments
 (0)