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
710A quick refresher: you can match against literals directly, and ` _ ` acts as an
8- * any* case:
11+ ‘ any’ case:
912
10- ``` { rust}
13+ ``` rust
1114let x = 1 ;
1215
1316match x {
@@ -18,9 +21,11 @@ match x {
1821}
1922```
2023
24+ # Multiple patterns
25+
2126You can match multiple patterns with ` | ` :
2227
23- ``` { rust}
28+ ``` rust
2429let x = 1 ;
2530
2631match x {
@@ -30,9 +35,11 @@ match x {
3035}
3136```
3237
38+ # Ranges
39+
3340You can match a range of values with ` ... ` :
3441
35- ``` { rust}
42+ ``` rust
3643let x = 1 ;
3744
3845match x {
@@ -43,10 +50,12 @@ match x {
4350
4451Ranges 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
4756the value to a name with ` @ ` :
4857
49- ``` { rust}
58+ ``` rust
5059let x = 1 ;
5160
5261match 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
5970ignore the value and type in the variant:
6071
61- ``` { rust}
72+ ``` rust
6273enum 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
7891enum 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
110110let x = 5 ;
111111
112112match x {
113113 ref r => println! (" Got a reference to {}" , r ),
114114}
115115```
116116
117+ [ ref ] : references-and-borrowing.html
118+
117119Here, the ` r ` inside the ` match ` has the type ` &i32 ` . In other words, the ` ref `
118120keyword _ creates_ a reference, for use in the pattern. If you need a mutable
119121reference, ` ref mut ` will work in the same way:
120122
121- ``` { rust}
123+ ``` rust
122124let mut x = 5 ;
123125
124126match 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
133137struct 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 don’ t have to give them all names:
146150
147- ``` {rust}
148- # #![allow(non_shorthand_field_patterns)]
151+ ``` rust
149152struct Point {
150153 x : i32 ,
151154 y : i32 ,
@@ -160,8 +163,7 @@ match origin {
160163
161164You can do this kind of match on any member, not just the first:
162165
163- ``` {rust}
164- # #![allow(non_shorthand_field_patterns)]
166+ ``` rust
165167struct 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! 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:
193189
194190``` {rust,ignore}
195191match x {
0 commit comments