diff options
| author | Bjorn Tipling <bjorn@ambientchill.com> | 2017-01-15 12:27:41 -0800 |
|---|---|---|
| committer | Bjorn Tipling <bjorn@ambientchill.com> | 2017-01-15 12:27:41 -0800 |
| commit | 0dad9dcf9ec7284283ef97dd0f192060a70cfad5 (patch) | |
| tree | aa019f792cb9d11855732e2863fe6c54bd55332a | |
| parent | c21f73e2db3ddaf29bb768fcbcd2b073554ebab1 (diff) | |
| download | rust-0dad9dcf9ec7284283ef97dd0f192060a70cfad5.tar.gz rust-0dad9dcf9ec7284283ef97dd0f192060a70cfad5.zip | |
An update to patterns documentation
As it is written it creates a lot of confusion.
| -rw-r--r-- | src/doc/book/patterns.md | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/doc/book/patterns.md b/src/doc/book/patterns.md index b50fa01b8e2..24f71608a5b 100644 --- a/src/doc/book/patterns.md +++ b/src/doc/book/patterns.md @@ -23,6 +23,33 @@ match x { This prints `one`. +It's possible to create a binding for the value in the any case: + +```rust +let x = 1; + +match x { + y => println!("x: {} y: {}", x, y), +} +``` + +This prints: + +```text +x: 1 y: 1 +``` + +Note it is an error to have both a catch-all `_` and a catch-all binding in the same match block: + +```rust +let x = 1; + +match x { + y => println!("x: {} y: {}", x, y), + _ => println!("anything"), // this causes an error as it is unreachable +} +``` + There’s one pitfall with patterns: like anything that introduces a new binding, they introduce shadowing. For example: |
