1
1
% Patterns
2
2
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
6
9
7
10
A quick refresher: you can match against literals directly, and ` _ ` acts as an
8
- * any* case:
11
+ ‘ any’ case:
9
12
10
- ``` { rust}
13
+ ``` rust
11
14
let x = 1 ;
12
15
13
16
match x {
@@ -18,9 +21,11 @@ match x {
18
21
}
19
22
```
20
23
24
+ # Multiple patterns
25
+
21
26
You can match multiple patterns with ` | ` :
22
27
23
- ``` { rust}
28
+ ``` rust
24
29
let x = 1 ;
25
30
26
31
match x {
@@ -30,9 +35,11 @@ match x {
30
35
}
31
36
```
32
37
38
+ # Ranges
39
+
33
40
You can match a range of values with ` ... ` :
34
41
35
- ``` { rust}
42
+ ``` rust
36
43
let x = 1 ;
37
44
38
45
match x {
@@ -43,10 +50,12 @@ match x {
43
50
44
51
Ranges are mostly used with integers and single characters.
45
52
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
47
56
the value to a name with ` @ ` :
48
57
49
- ``` { rust}
58
+ ``` rust
50
59
let x = 1 ;
51
60
52
61
match x {
@@ -55,10 +64,12 @@ match x {
55
64
}
56
65
```
57
66
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
59
70
ignore the value and type in the variant:
60
71
61
- ``` { rust}
72
+ ``` rust
62
73
enum OptionalInt {
63
74
Value (i32 ),
64
75
Missing ,
@@ -72,9 +83,11 @@ match x {
72
83
}
73
84
```
74
85
75
- You can introduce * match guards* with ` if ` :
86
+ # Guards
87
+
88
+ You can introduce ‘match guards’ with ` if ` :
76
89
77
- ``` { rust}
90
+ ``` rust
78
91
enum OptionalInt {
79
92
Value (i32 ),
80
93
Missing ,
@@ -89,47 +102,38 @@ match x {
89
102
}
90
103
```
91
104
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
106
106
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:
108
108
109
- ``` { rust}
109
+ ``` rust
110
110
let x = 5 ;
111
111
112
112
match x {
113
113
ref r => println! (" Got a reference to {}" , r ),
114
114
}
115
115
```
116
116
117
+ [ ref ] : references-and-borrowing.html
118
+
117
119
Here, the ` r ` inside the ` match ` has the type ` &i32 ` . In other words, the ` ref `
118
120
keyword _ creates_ a reference, for use in the pattern. If you need a mutable
119
121
reference, ` ref mut ` will work in the same way:
120
122
121
- ``` { rust}
123
+ ``` rust
122
124
let mut x = 5 ;
123
125
124
126
match x {
125
127
ref mut mr => println! (" Got a mutable reference to {}" , mr ),
126
128
}
127
129
```
128
130
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:
130
135
131
- ``` {rust}
132
- # #![allow(non_shorthand_field_patterns)]
136
+ ``` rust
133
137
struct Point {
134
138
x : i32 ,
135
139
y : i32 ,
@@ -142,10 +146,9 @@ match origin {
142
146
}
143
147
```
144
148
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 don’ t have to give them all names:
146
150
147
- ``` {rust}
148
- # #![allow(non_shorthand_field_patterns)]
151
+ ``` rust
149
152
struct Point {
150
153
x : i32 ,
151
154
y : i32 ,
@@ -160,8 +163,7 @@ match origin {
160
163
161
164
You can do this kind of match on any member, not just the first:
162
165
163
- ``` {rust}
164
- # #![allow(non_shorthand_field_patterns)]
166
+ ``` rust
165
167
struct Point {
166
168
x : i32 ,
167
169
y : i32 ,
@@ -174,22 +176,16 @@ match origin {
174
176
}
175
177
```
176
178
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 ] .
178
181
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
183
184
184
- match &v[..] {
185
- ["match_this", second] => println!("The second element is {}", second),
186
- _ => {},
187
- }
188
- }
189
- ```
185
+ # Mix and Match
190
186
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! That’ s a lot of different ways to match things, and they can all be
188
+ mixed and matched, depending on what you’ re doing:
193
189
194
190
``` {rust,ignore}
195
191
match x {
0 commit comments