]> git.proxmox.com Git - rustc.git/blame - src/doc/reference/src/expressions/match-expr.md
New upstream version 1.55.0+dfsg1
[rustc.git] / src / doc / reference / src / expressions / match-expr.md
CommitLineData
ea8adc8c
XL
1# `match` expressions
2
8faf50e0
XL
3> **<sup>Syntax</sup>**\
4> _MatchExpression_ :\
136023e0 5> &nbsp;&nbsp; `match` _Scrutinee_ `{`\
8faf50e0
XL
6> &nbsp;&nbsp; &nbsp;&nbsp; [_InnerAttribute_]<sup>\*</sup>\
7> &nbsp;&nbsp; &nbsp;&nbsp; _MatchArms_<sup>?</sup>\
8> &nbsp;&nbsp; `}`
0531ce1d 9>
136023e0
XL
10>_Scrutinee_ :\
11> &nbsp;&nbsp; [_Expression_]<sub>_except struct expression_</sub>
12>
8faf50e0 13> _MatchArms_ :\
0531ce1d 14> &nbsp;&nbsp; ( _MatchArm_ `=>`
ba9703b0
XL
15> ( [_ExpressionWithoutBlock_][_Expression_] `,`
16> | [_ExpressionWithBlock_][_Expression_] `,`<sup>?</sup> )
8faf50e0 17> )<sup>\*</sup>\
ba9703b0 18> &nbsp;&nbsp; _MatchArm_ `=>` [_Expression_] `,`<sup>?</sup>
0531ce1d 19>
8faf50e0 20> _MatchArm_ :\
cdc7bbd5 21> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> [_Pattern_] _MatchArmGuard_<sup>?</sup>
0531ce1d 22>
8faf50e0 23> _MatchArmGuard_ :\
0531ce1d 24> &nbsp;&nbsp; `if` [_Expression_]
2c00a5a8 25
6a06907d
XL
26A *`match` expression* branches on a pattern.
27The exact form of matching that occurs depends on the [pattern].
28A `match` expression has a *[scrutinee] expression*, which is the value to compare to the patterns.
29The scrutinee expression and the patterns must have the same type.
30
31A `match` behaves differently depending on whether or not the scrutinee expression is a [place expression or value expression][place expression].
32If the scrutinee expression is a [value expression], it is first evaluated into a temporary location, and the resulting value is sequentially compared to the patterns in the arms until a match is found.
33The first arm with a matching pattern is chosen as the branch target of the `match`, any variables bound by the pattern are assigned to local variables in the arm's block, and control enters the block.
34
35When the scrutinee expression is a [place expression], the match does not allocate a temporary location;
36however, a by-value binding may copy or move from the memory location.
37When possible, it is preferable to match on place expressions, as the lifetime of these matches inherits the lifetime of the place expression rather than being restricted to the inside of the match.
ea8adc8c
XL
38
39An example of a `match` expression:
40
41```rust
42let x = 1;
43
44match x {
45 1 => println!("one"),
46 2 => println!("two"),
47 3 => println!("three"),
48 4 => println!("four"),
49 5 => println!("five"),
50 _ => println!("something else"),
51}
52```
53
6a06907d
XL
54Variables bound within the pattern are scoped to the match guard and the arm's expression.
55The [binding mode] (move, copy, or reference) depends on the pattern.
ea8adc8c 56
6a06907d
XL
57Multiple match patterns may be joined with the `|` operator.
58Each pattern will be tested in left-to-right sequence until a successful match is found.
ea8adc8c
XL
59
60```rust
1b1a35ee 61let x = 9;
ea8adc8c
XL
62let message = match x {
63 0 | 1 => "not many",
0531ce1d 64 2 ..= 9 => "a few",
ea8adc8c
XL
65 _ => "lots"
66};
0531ce1d
XL
67
68assert_eq!(message, "a few");
532ac7d7
XL
69
70// Demonstration of pattern match order.
71struct S(i32, i32);
72
73match S(1, 2) {
74 S(z @ 1, _) | S(_, z @ 2) => assert_eq!(z, 1),
75 _ => panic!(),
76}
ea8adc8c
XL
77```
78
6a06907d
XL
79> Note: The `2..=9` is a [Range Pattern], not a [Range Expression].
80> Thus, only those types of ranges supported by range patterns can be used in match arms.
532ac7d7 81
6a06907d
XL
82Every binding in each `|` separated pattern must appear in all of the patterns in the arm.
83Every binding of the same name must have the same type, and have the same binding mode.
ea8adc8c 84
60c5eb7d
XL
85## Match guards
86
6a06907d
XL
87Match arms can accept _match guards_ to further refine the criteria for matching a case.
88Pattern guards appear after the pattern and consist of a `bool`-typed expression following the `if` keyword.
ea8adc8c 89
b7449926 90When the pattern matches successfully, the pattern guard expression is executed.
532ac7d7 91If the expression evaluates to true, the pattern is successfully matched against.
6a06907d 92Otherwise, the next pattern, including other matches with the `|` operator in the same arm, is tested.
b7449926 93
ea8adc8c
XL
94```rust
95# let maybe_digit = Some(0);
96# fn process_digit(i: i32) { }
97# fn process_other(i: i32) { }
98let message = match maybe_digit {
99 Some(x) if x < 10 => process_digit(x),
100 Some(x) => process_other(x),
101 None => panic!(),
102};
103```
104
6a06907d
XL
105> Note: Multiple matches using the `|` operator can cause the pattern guard and the side effects it has to execute multiple times.
106> For example:
b7449926
XL
107>
108> ```rust
532ac7d7
XL
109> # use std::cell::Cell;
110> let i : Cell<i32> = Cell::new(0);
111> match 1 {
112> 1 | _ if { i.set(i.get() + 1); false } => {}
113> _ => {}
b7449926 114> }
532ac7d7 115> assert_eq!(i.get(), 2);
b7449926
XL
116> ```
117
60c5eb7d 118A pattern guard may refer to the variables bound within the pattern they follow.
6a06907d
XL
119Before evaluating the guard, a shared reference is taken to the part of the scrutinee the variable matches on.
120While evaluating the guard, this shared reference is then used when accessing the variable.
121Only when the guard evaluates to true is the value moved, or copied, from the scrutinee into the variable.
122This allows shared borrows to be used inside guards without moving out of the scrutinee in case guard fails to match.
123Moreover, by holding a shared reference while evaluating the guard, mutation inside guards is also prevented.
60c5eb7d 124
8faf50e0
XL
125## Attributes on match arms
126
6a06907d
XL
127Outer attributes are allowed on match arms.
128The only attributes that have meaning on match arms are [`cfg`], [`cold`], and the [lint check attributes].
8faf50e0 129
6a06907d 130[Inner attributes] are allowed directly after the opening brace of the match expression in the same expression contexts as [attributes on block expressions].
13cf67c4 131
416331ca 132[_Expression_]: ../expressions.md
416331ca
XL
133[place expression]: ../expressions.md#place-expressions-and-value-expressions
134[value expression]: ../expressions.md#place-expressions-and-value-expressions
135[_InnerAttribute_]: ../attributes.md
136[_OuterAttribute_]: ../attributes.md
137[`cfg`]: ../conditional-compilation.md
138[`cold`]: ../attributes/codegen.md#the-cold-attribute
139[lint check attributes]: ../attributes/diagnostics.md#lint-check-attributes
140[Range Expression]: range-expr.md
141
142[_Pattern_]: ../patterns.md
143[pattern]: ../patterns.md
144[Inner attributes]: ../attributes.md
145[Range Pattern]: ../patterns.md#range-patterns
146[attributes on block expressions]: block-expr.md#attributes-on-block-expressions
147[binding mode]: ../patterns.md#binding-modes
148[scrutinee]: ../glossary.md#scrutinee