Skip to content

Commit 9661efa

Browse files
committed
TRPL editing: patterns
Partially addresses #24388
1 parent 836c8a8 commit 9661efa

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

src/doc/trpl/patterns.md

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ match x {
2121
}
2222
```
2323

24+
This prints `one`.
25+
2426
# Multiple patterns
2527

2628
You can match multiple patterns with `|`:
@@ -35,6 +37,8 @@ match x {
3537
}
3638
```
3739

40+
This prints `one or two`.
41+
3842
# Ranges
3943

4044
You can match a range of values with `...`:
@@ -48,7 +52,21 @@ match x {
4852
}
4953
```
5054

51-
Ranges are mostly used with integers and single characters.
55+
This prints `one through five`.
56+
57+
Ranges are mostly used with integers and `char`s:
58+
59+
```rust
60+
let x = '💅';
61+
62+
match x {
63+
'a' ... 'j' => println!("early letter"),
64+
'k' ... 'z' => println!("late letter"),
65+
_ => println!("something else"),
66+
}
67+
```
68+
69+
This prints `something else`
5270

5371
# Bindings
5472

@@ -64,6 +82,8 @@ match x {
6482
}
6583
```
6684

85+
This prints `got a range element 1`.
86+
6787
# Ignoring variants
6888

6989
If you’re matching on an enum which has variants, you can use `..` to
@@ -83,6 +103,8 @@ match x {
83103
}
84104
```
85105

106+
This prints `Got an int!`.
107+
86108
# Guards
87109

88110
You can introduce ‘match guards’ with `if`:
@@ -102,6 +124,8 @@ match x {
102124
}
103125
```
104126

127+
This prints `Got an int!`
128+
105129
# ref and ref mut
106130

107131
If you want to get a [reference][ref], use the `ref` keyword:
@@ -114,6 +138,8 @@ match x {
114138
}
115139
```
116140

141+
This prints `Got a reference to 5`.
142+
117143
[ref]: references-and-borrowing.html
118144

119145
Here, the `r` inside the `match` has the type `&i32`. In other words, the `ref`
@@ -130,7 +156,7 @@ match x {
130156

131157
# Destructuring
132158

133-
If you have a compound data type, like a `struct`, you can destructure it
159+
If you have a compound data type, like a [`struct`][struct], you can destructure it
134160
inside of a pattern:
135161

136162
```rust
@@ -146,6 +172,8 @@ match origin {
146172
}
147173
```
148174

175+
[struct]: structs.html
176+
149177
If we only care about some of the values, we don’t have to give them all names:
150178

151179
```rust
@@ -161,6 +189,8 @@ match origin {
161189
}
162190
```
163191

192+
This prints `x is 0`.
193+
164194
You can do this kind of match on any member, not just the first:
165195

166196
```rust
@@ -176,6 +206,8 @@ match origin {
176206
}
177207
```
178208

209+
This prints `y is 0`.
210+
179211
This ‘destructuring’ behavior works on any compound data type, like
180212
[tuples][tuples] or [enums][enums].
181213

@@ -187,10 +219,10 @@ This ‘destructuring’ behavior works on any compound data type, like
187219
Whew! That’s a lot of different ways to match things, and they can all be
188220
mixed and matched, depending on what you’re doing:
189221

190-
```{rust,ignore}
222+
```rust,ignore
191223
match x {
192224
Foo { x: Some(ref name), y: None } => ...
193225
}
194226
```
195227

196-
Patterns are very powerful. Make good use of them.
228+
Patterns are very powerful. Make good use of them.

0 commit comments

Comments
 (0)