Skip to content

Commit 0dad9dc

Browse files
committed
An update to patterns documentation
As it is written it creates a lot of confusion.
1 parent c21f73e commit 0dad9dc

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/doc/book/patterns.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,33 @@ match x {
2323

2424
This prints `one`.
2525

26+
It's possible to create a binding for the value in the any case:
27+
28+
```rust
29+
let x = 1;
30+
31+
match x {
32+
y => println!("x: {} y: {}", x, y),
33+
}
34+
```
35+
36+
This prints:
37+
38+
```text
39+
x: 1 y: 1
40+
```
41+
42+
Note it is an error to have both a catch-all `_` and a catch-all binding in the same match block:
43+
44+
```rust
45+
let x = 1;
46+
47+
match x {
48+
y => println!("x: {} y: {}", x, y),
49+
_ => println!("anything"), // this causes an error as it is unreachable
50+
}
51+
```
52+
2653
There’s one pitfall with patterns: like anything that introduces a new binding,
2754
they introduce shadowing. For example:
2855

0 commit comments

Comments
 (0)