Skip to content

Commit bbc2056

Browse files
committed
Comment on shadowing with patterns
Fixes #28687
1 parent c056cbb commit bbc2056

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/doc/trpl/patterns.md

+25
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,31 @@ match x {
2323

2424
This prints `one`.
2525

26+
There’s one pitfall with patterns: like anything that introduces a new binding,
27+
they introduce shadowing. For example:
28+
29+
```rust
30+
let x = 'x';
31+
let c = 'c';
32+
33+
match c {
34+
x => println!("x: {} c: {}", x, c),
35+
}
36+
37+
println!("x: {}", x)
38+
```
39+
40+
This prints:
41+
42+
```text
43+
x: c c: c
44+
x: x
45+
```
46+
47+
In other words, `x =>` matches the pattern and introduces a new binding named
48+
`x` that’s in scope for the match arm. Because we already have a binding named
49+
`x`, this new `x` shadows it.
50+
2651
# Multiple patterns
2752

2853
You can match multiple patterns with `|`:

0 commit comments

Comments
 (0)