Skip to content

Commit 5f841eb

Browse files
committed
Rollup merge of rust-lang#27460 - JanLikar:master, r=steveklabnik
- Fix rust-lang#26968 by noting the difference between ".." and "_" more explicitly - Change one of the examples to show the match-all behaviour of ".." - Merge "Ignoring variants" and "Ignoring bindings" sections into the latter r? @steveklabnik
2 parents f7eecc9 + 91972ba commit 5f841eb

File tree

1 file changed

+38
-41
lines changed

1 file changed

+38
-41
lines changed

src/doc/trpl/patterns.md

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -112,26 +112,55 @@ match x {
112112
}
113113
```
114114

115-
# Ignoring variants
115+
# Ignoring bindings
116116

117-
If you’re matching on an enum which has variants, you can use `..` to
118-
ignore the value and type in the variant:
117+
You can use `_` in a pattern to disregard the type and value.
118+
For example, here’s a `match` against a `Result<T, E>`:
119119

120120
```rust
121-
enum OptionalInt {
122-
Value(i32),
121+
# let some_value: Result<i32, &'static str> = Err("There was an error");
122+
match some_value {
123+
Ok(value) => println!("got a value: {}", value),
124+
Err(_) => println!("an error occurred"),
125+
}
126+
```
127+
128+
In the first arm, we bind the value inside the `Ok` variant to `value`. But
129+
in the `Err` arm, we use `_` to disregard the specific error, and just print
130+
a general error message.
131+
132+
`_` is valid in any pattern that creates a binding. This can be useful to
133+
ignore parts of a larger structure:
134+
135+
```rust
136+
fn coordinate() -> (i32, i32, i32) {
137+
// generate and return some sort of triple tuple
138+
# (1, 2, 3)
139+
}
140+
141+
let (x, _, z) = coordinate();
142+
```
143+
144+
Here, we bind the first and last element of the tuple to `x` and `z`, but
145+
ignore the middle element.
146+
147+
Similarly, you can use `..` in a pattern to disregard multiple values.
148+
149+
```rust
150+
enum OptionalTuple {
151+
Value(i32, i32, i32),
123152
Missing,
124153
}
125154

126-
let x = OptionalInt::Value(5);
155+
let x = OptionalTuple::Value(5, -2, 3);
127156

128157
match x {
129-
OptionalInt::Value(..) => println!("Got an int!"),
130-
OptionalInt::Missing => println!("No such luck."),
158+
OptionalTuple::Value(..) => println!("Got a tuple!"),
159+
OptionalTuple::Missing => println!("No such luck."),
131160
}
132161
```
133162

134-
This prints `Got an int!`.
163+
This prints `Got a tuple!`.
135164

136165
# Guards
137166

@@ -282,38 +311,6 @@ This ‘destructuring’ behavior works on any compound data type, like
282311
[tuples]: primitive-types.html#tuples
283312
[enums]: enums.html
284313

285-
# Ignoring bindings
286-
287-
You can use `_` in a pattern to disregard the value. For example, here’s a
288-
`match` against a `Result<T, E>`:
289-
290-
```rust
291-
# let some_value: Result<i32, &'static str> = Err("There was an error");
292-
match some_value {
293-
Ok(value) => println!("got a value: {}", value),
294-
Err(_) => println!("an error occurred"),
295-
}
296-
```
297-
298-
In the first arm, we bind the value inside the `Ok` variant to `value`. But
299-
in the `Err` arm, we use `_` to disregard the specific error, and just print
300-
a general error message.
301-
302-
`_` is valid in any pattern that creates a binding. This can be useful to
303-
ignore parts of a larger structure:
304-
305-
```rust
306-
fn coordinate() -> (i32, i32, i32) {
307-
// generate and return some sort of triple tuple
308-
# (1, 2, 3)
309-
}
310-
311-
let (x, _, z) = coordinate();
312-
```
313-
314-
Here, we bind the first and last element of the tuple to `x` and `z`, but
315-
ignore the middle element.
316-
317314
# Mix and Match
318315

319316
Whew! That’s a lot of different ways to match things, and they can all be

0 commit comments

Comments
 (0)