Explain interaction with if and | in patterns

Fixes #26012
This commit is contained in:
Steve Klabnik 2015-06-09 14:32:10 -04:00
parent 7b0f2af27f
commit a0b08f387b

View file

@ -154,6 +154,31 @@ match x {
This prints `Got an int!`.
If youre using `if` with multiple patterns, the `if` applies to both sides:
```rust
let x = 4;
let y = false;
match x {
4 | 5 if y => println!("yes"),
_ => println!("no"),
}
```
This prints `no`, because the `if` applies to the whole of `4 | 5`, and not to
just the `5`, In other words, the the precedence of `if` behaves like this:
```text
(4 | 5) if y => ...
```
not this:
```text
4 | (5 if y) => ...
```
# ref and ref mut
If you want to get a [reference][ref], use the `ref` keyword: