Skip to content

Commit 0003310

Browse files
brauliobzehuss
authored andcommitted
WIP Patterns chapter:
- Added the chapter about patterns, with some explanations and the grammar. - Had to move some documentation about patterns from the `match` section. - Removed the bit about the new syntax of inclusive pattern ranges (a..=b), since it is not stable nor in the process of being stabilized.
1 parent 221ea65 commit 0003310

File tree

3 files changed

+625
-92
lines changed

3 files changed

+625
-92
lines changed

src/SUMMARY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
- [Match expressions](expressions/match-expr.md)
6464
- [Return expressions](expressions/return-expr.md)
6565

66+
- [Patterns](patterns.md)
67+
6668
- [Type system](type-system.md)
6769
- [Types](types.md)
6870
- [Dynamically Sized Types](dynamically-sized-types.md)

src/expressions/match-expr.md

Lines changed: 21 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,15 @@
1818
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> _MatchArmPatterns_ _MatchArmGuard_<sup>?</sup>
1919
>
2020
> _MatchArmPatterns_ :\
21-
> &nbsp;&nbsp; `|`<sup>?</sup> _Pattern_ ( `|` _Pattern_ )<sup>\*</sup>
21+
> &nbsp;&nbsp; `|`<sup>?</sup> [_Pattern_] ( `|` [_Pattern_] )<sup>\*</sup>
2222
>
2323
> _MatchArmGuard_ :\
2424
> &nbsp;&nbsp; `if` [_Expression_]
2525
2626
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.
3330

3431
A `match` behaves differently depending on whether or not the head expression
3532
is a [place expression or value expression][place expression].
@@ -65,102 +62,28 @@ match x {
6562
Patterns that bind variables default to binding to a copy or move of the
6663
matched value (depending on the matched value's type). This can be changed to
6764
bind to a reference by using the `ref` keyword, or to a mutable reference using
68-
`ref mut`.
65+
`ref mut`. See [Identifier Patterns].
6966

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:
12468

12569
```rust
12670
# let x = 9;
12771
let message = match x {
12872
0 | 1 => "not many",
129-
2 ..= 9 => "a few",
73+
2 ... 9 => "a few",
13074
_ => "lots"
13175
};
13276

13377
assert_eq!(message, "a few");
13478
```
13579

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.
14383

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`.
16285

163-
Finally, match arms can accept *pattern guards* to further refine the
86+
Match arms can accept _match guards_ to further refine the
16487
criteria for matching a case. Pattern guards appear after the pattern and
16588
consist of a bool-typed expression following the `if` keyword. A pattern guard
16689
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].
205128
[_BlockExpression_]: expressions/block-expr.html#block-expressions
206129
[place expression]: expressions.html#place-expressions-and-value-expressions
207130
[value expression]: expressions.html#place-expressions-and-value-expressions
208-
[`char`]: types.html#textual-types
209-
[numeric types]: types.html#numeric-types
210131
[_InnerAttribute_]: attributes.html
211132
[_OuterAttribute_]: attributes.html
212133
[`cfg`]: conditional-compilation.html
213134
[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

Comments
 (0)