Skip to content

Commit a0b08f3

Browse files
committed
Explain interaction with if and | in patterns
Fixes #26012
1 parent 7b0f2af commit a0b08f3

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/doc/trpl/patterns.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,31 @@ match x {
154154

155155
This prints `Got an int!`.
156156

157+
If you’re using `if` with multiple patterns, the `if` applies to both sides:
158+
159+
```rust
160+
let x = 4;
161+
let y = false;
162+
163+
match x {
164+
4 | 5 if y => println!("yes"),
165+
_ => println!("no"),
166+
}
167+
```
168+
169+
This prints `no`, because the `if` applies to the whole of `4 | 5`, and not to
170+
just the `5`, In other words, the the precedence of `if` behaves like this:
171+
172+
```text
173+
(4 | 5) if y => ...
174+
```
175+
176+
not this:
177+
178+
```text
179+
4 | (5 if y) => ...
180+
```
181+
157182
# ref and ref mut
158183

159184
If you want to get a [reference][ref], use the `ref` keyword:

0 commit comments

Comments
 (0)