Skip to content

An update to patterns documentation #39084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/doc/book/patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down