Skip to content

Commit 88267c9

Browse files
committed
Add the numbered match ergonomics rules adopted by lang
In the lang design meeting on 2024-06-26, we adopted a slate of five rules for match ergonomics. We later amended *Rule 4* in our meeting on 2024-07-03. These five rules, as amended, form the normative description of what the lang team means for the rules to be. Therefore, in the reference-level explanation, let's include these rules verbatim and note that the remaining sections describe these rules further.
1 parent 97bf386 commit 88267c9

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

text/3627-match-ergonomics-2024.md

+27
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,33 @@ let _: &u8 = x;
136136
This explanation assumes familiarity with the current match ergonomics rules,
137137
including the "default binding mode" terminology. Refer to [RFC 2005](./2005-match-ergonomics.md#detailed-design).
138138

139+
## The rules in brief
140+
141+
Building on the rules of [RFC 2005](./2005-match-ergonomics.md), this RFC adopts
142+
the following five rules for match ergonomics:
143+
144+
- **Rule 1**: When the DBM (default binding mode) is not `move` (whether or not
145+
behind a reference), writing `mut` on a binding is an error.
146+
- **Rule 2**: When a reference pattern matches against a reference, do not
147+
update the DBM.
148+
- **Rule 3**: If we've previously matched against a shared reference in the
149+
scrutinee (or against a `ref` DBM under *Rule 4*, or against a mutable
150+
reference treated as a shared one or a `ref mut` DBM treated as a `ref` one
151+
under *Rule 5*), set the DBM to `ref` whenever we would otherwise set it to
152+
`ref mut`.
153+
- **Rule 4**: If an `&` pattern is being matched against a non-reference type or
154+
an `&mut` pattern is being matched against a shared reference type or a
155+
non-reference type, **and if** the DBM is `ref` or `ref mut`, match the
156+
pattern against the DBM as though it were a type.
157+
- **Rule 5**: If an `&` pattern is being matched against a mutable reference
158+
type (or against a `ref mut` DBM under *Rule 4*), act as if the type were a
159+
shared reference instead (or that the `ref mut` DBM is a `ref` DBM instead).
160+
161+
*Rule 1* and *Rule 2* are edition-dependent and will be stabilized with
162+
Rust 2024. The other three rules will be stabilized in all editions.
163+
164+
In the sections below, we describe these rules and their effects in more detail.
165+
139166
## Edition 2024: `mut` does not reset binding mode to by-value
140167

141168
In the new edition, `mut` no longer resets the binding mode to by-value;

0 commit comments

Comments
 (0)