|
18 | 18 | > [_OuterAttribute_]<sup>\*</sup> _MatchArmPatterns_ _MatchArmGuard_<sup>?</sup>
|
19 | 19 | >
|
20 | 20 | > _MatchArmPatterns_ :\
|
21 |
| -> `|`<sup>?</sup> _Pattern_ ( `|` _Pattern_ )<sup>\*</sup> |
| 21 | +> `|`<sup>?</sup> [_Pattern_] ( `|` [_Pattern_] )<sup>\*</sup> |
22 | 22 | >
|
23 | 23 | > _MatchArmGuard_ :\
|
24 | 24 | > `if` [_Expression_]
|
25 | 25 |
|
26 | 26 | A *`match` expression* branches on a pattern. The exact form of matching that
|
27 |
| -occurs depends on the pattern. *Patterns* consist of some combination of |
28 |
| -literals, destructured arrays or enum constructors, structs and tuples, |
29 |
| -variable binding specifications, wildcards (`..`), and placeholders (`_`). A |
30 |
| -`match` expression has a *head expression*, which is the value to compare to |
31 |
| -the patterns. The type of the patterns must equal the type of the head |
32 |
| -expression. |
| 27 | +occurs depends on the pattern. See [Patterns] for more details. A `match` |
| 28 | +expression has a *head expression*, which is the value to compare to the |
| 29 | +patterns. The type of the patterns must equal the type of the head expression. |
33 | 30 |
|
34 | 31 | A `match` behaves differently depending on whether or not the head expression
|
35 | 32 | is a [place expression or value expression][place expression].
|
@@ -65,102 +62,28 @@ match x {
|
65 | 62 | Patterns that bind variables default to binding to a copy or move of the
|
66 | 63 | matched value (depending on the matched value's type). This can be changed to
|
67 | 64 | bind to a reference by using the `ref` keyword, or to a mutable reference using
|
68 |
| -`ref mut`. |
| 65 | +`ref mut`. See [Identifier Patterns]. |
69 | 66 |
|
70 |
| -Patterns can be used to *destructure* structs, enums, and tuples. Destructuring |
71 |
| -breaks a value up into its component pieces. The syntax used is the same as |
72 |
| -when creating such values. When destructing a data structure with named (but |
73 |
| -not numbered) fields, it is allowed to write `fieldname` as a shorthand for |
74 |
| -`fieldname: fieldname`. In a pattern whose head expression has a `struct`, |
75 |
| -`enum` or `tupl` type, a placeholder (`_`) stands for a *single* data field, |
76 |
| -whereas a wildcard `..` stands for *all* the fields of a particular variant. |
77 |
| - |
78 |
| -```rust |
79 |
| -# enum Message { |
80 |
| -# Quit, |
81 |
| -# WriteString(String), |
82 |
| -# Move { x: i32, y: i32 }, |
83 |
| -# ChangeColor(u8, u8, u8), |
84 |
| -# } |
85 |
| -# let message = Message::Quit; |
86 |
| -match message { |
87 |
| - Message::Quit => println!("Quit"), |
88 |
| - Message::WriteString(write) => println!("{}", &write), |
89 |
| - Message::Move{ x, y: 0 } => println!("move {} horizontally", x), |
90 |
| - Message::Move{ .. } => println!("other move"), |
91 |
| - Message::ChangeColor { 0: red, 1: green, 2: _ } => { |
92 |
| - println!("color change, red: {}, green: {}", red, green); |
93 |
| - } |
94 |
| -}; |
95 |
| -``` |
96 |
| - |
97 |
| -Patterns can also dereference pointers by using the `&`, `&mut` and `box` |
98 |
| -symbols, as appropriate. For example, these two matches on `x: &i32` are |
99 |
| -equivalent: |
100 |
| - |
101 |
| -```rust |
102 |
| -let int_reference = &3; |
103 |
| - |
104 |
| -let a = match *int_reference { 0 => "zero", _ => "some" }; |
105 |
| -let b = match int_reference { &0 => "zero", _ => "some" }; |
106 |
| - |
107 |
| -assert_eq!(a, b); |
108 |
| -``` |
109 |
| - |
110 |
| -Subpatterns can also be bound to variables by the use of the syntax `variable @ |
111 |
| -subpattern`. For example: |
112 |
| - |
113 |
| -```rust |
114 |
| -let x = 1; |
115 |
| - |
116 |
| -match x { |
117 |
| - e @ 1 ... 5 => println!("got a range element {}", e), |
118 |
| - _ => println!("anything"), |
119 |
| -} |
120 |
| -``` |
121 |
| - |
122 |
| -Multiple match patterns may be joined with the `|` operator. An inclusive range |
123 |
| -of values may be specified with `..=`. For example: |
| 67 | +Multiple match patterns may be joined with the `|` operator: |
124 | 68 |
|
125 | 69 | ```rust
|
126 | 70 | # let x = 9;
|
127 | 71 | let message = match x {
|
128 | 72 | 0 | 1 => "not many",
|
129 |
| - 2 ..= 9 => "a few", |
| 73 | + 2 ... 9 => "a few", |
130 | 74 | _ => "lots"
|
131 | 75 | };
|
132 | 76 |
|
133 | 77 | assert_eq!(message, "a few");
|
134 | 78 | ```
|
135 | 79 |
|
136 |
| -Other forms of [range] \(e.g `..` for an exclusive range, or any range with one or |
137 |
| -both endpoints left unspecified) are not supported in matches. The |
138 |
| -syntax `...` is also accepted for inclusive ranges in patterns only, for |
139 |
| -backwards compatibility. |
140 |
| - |
141 |
| -Range patterns only work with [`char`] and [numeric types]. A range pattern may |
142 |
| -not be a sub-range of another range pattern inside the same `match`. |
| 80 | +Please notice that the `2...9` is a [Range Pattern], not a [Range Expression] |
| 81 | +and, thus, only those types of ranges supported by range patterns can be used |
| 82 | +in match arms. |
143 | 83 |
|
144 |
| -Slice patterns can match both arrays of fixed size and slices of dynamic size. |
145 |
| -```rust |
146 |
| -// Fixed size |
147 |
| -let arr = [1, 2, 3]; |
148 |
| -match arr { |
149 |
| - [1, _, _] => "starts with one", |
150 |
| - [a, b, c] => "starts with something else", |
151 |
| -}; |
152 |
| -``` |
153 |
| -```rust |
154 |
| -// Dynamic size |
155 |
| -let v = vec![1, 2, 3]; |
156 |
| -match v[..] { |
157 |
| - [a, b] => { /* this arm will not apply because the length doesn't match */ } |
158 |
| - [a, b, c] => { /* this arm will apply */ } |
159 |
| - _ => { /* this wildcard is required, since we don't know length statically */ } |
160 |
| -} |
161 |
| -``` |
| 84 | +A range pattern may not be a sub-range of another range pattern inside the same `match`. |
162 | 85 |
|
163 |
| -Finally, match arms can accept *pattern guards* to further refine the |
| 86 | +Match arms can accept _match guards_ to further refine the |
164 | 87 | criteria for matching a case. Pattern guards appear after the pattern and
|
165 | 88 | consist of a bool-typed expression following the `if` keyword. A pattern guard
|
166 | 89 | may refer to the variables bound within the pattern they follow.
|
@@ -205,10 +128,16 @@ meaning on match arms are [`cfg`], `cold`, and the [lint check attributes].
|
205 | 128 | [_BlockExpression_]: expressions/block-expr.html#block-expressions
|
206 | 129 | [place expression]: expressions.html#place-expressions-and-value-expressions
|
207 | 130 | [value expression]: expressions.html#place-expressions-and-value-expressions
|
208 |
| -[`char`]: types.html#textual-types |
209 |
| -[numeric types]: types.html#numeric-types |
210 | 131 | [_InnerAttribute_]: attributes.html
|
211 | 132 | [_OuterAttribute_]: attributes.html
|
212 | 133 | [`cfg`]: conditional-compilation.html
|
213 | 134 | [lint check attributes]: attributes.html#lint-check-attributes
|
214 |
| -[range]: expressions/range-expr.html |
| 135 | +[Range Expression]: expressions/range-expr.html |
| 136 | +
|
| 137 | +[_Pattern_]: patterns.html |
| 138 | +[Patterns]: patterns.html |
| 139 | +[Identifier Patterns]: patterns.html#identifier-patterns |
| 140 | +[Struct Patterns]: patterns.html#struct-patterns |
| 141 | +[Tuple Struct Patterns]: patterns.html#tuplestruct-patterns |
| 142 | +[Tuple Patterns]: patterns.html#tuple-patterns |
| 143 | +[Range Pattern]: patterns.html#range-patterns |
0 commit comments