Skip to content
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

ch18-03: Matching Named Variables: mention if let/while let #3110

Merged
merged 3 commits into from
Dec 12, 2024
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
31 changes: 16 additions & 15 deletions src/ch19-03-pattern-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ value.
### Matching Named Variables

Named variables are irrefutable patterns that match any value, and we’ve used
them many times in the book. However, there is a complication when you use
named variables in `match` expressions. Because `match` starts a new scope,
variables declared as part of a pattern inside the `match` expression will
shadow those with the same name outside the `match` construct, as is the case
with all variables. In Listing 19-11, we declare a variable named `x` with the
value `Some(5)` and a variable `y` with the value `10`. We then create a
`match` expression on the value `x`. Look at the patterns in the match arms and
`println!` at the end, and try to figure out what the code will print before
running this code or reading further.
them many times in the book. However, there is a complication when you use named
variables in `match`, `if let`, or `while let` expressions. Because each of
these kinds of expression starts a new scope, variables declared as part of a
pattern inside the expression will shadow those with the same name outside, as
is the case with all variables. In Listing 19-11, we declare a variable named
`x` with the value `Some(5)` and a variable `y` with the value `10`. We then
create a `match` expression on the value `x`. Look at the patterns in the match
arms and `println!` at the end, and try to figure out what the code will print
before running this code or reading further.

<Listing number="19-11" file-name="src/main.rs" caption="A `match` expression with an arm that introduces a new variable which shadows an existing variable `y`">

Expand Down Expand Up @@ -67,11 +67,10 @@ Guards”](#extra-conditionals-with-match-guards)<!-- ignore --> section.

### Multiple Patterns

In `match` expressions, you can match multiple patterns using the `|` syntax,
which is the pattern _or_ operator. For example, in the following code we match
the value of `x` against the match arms, the first of which has an _or_ option,
meaning if the value of `x` matches either of the values in that arm, that
arm’s code will run:
You can match multiple patterns using the `|` syntax, which is the pattern _or_
operator. For example, in the following code we match the value of `x` against
the match arms, the first of which has an _or_ option, meaning if the value of
`x` matches either of the values in that arm, that arm’s code will run:

```rust
{{#rustdoc_include ../listings/ch19-patterns-and-matching/no-listing-02-multiple-patterns/src/main.rs:here}}
Expand Down Expand Up @@ -452,7 +451,9 @@ compiler error because using `..` in two places like this is ambiguous.

A _match guard_ is an additional `if` condition, specified after the pattern in
a `match` arm, that must also match for that arm to be chosen. Match guards are
useful for expressing more complex ideas than a pattern alone allows.
useful for expressing more complex ideas than a pattern alone allows. They are
only available in `match` expressions, not in `if let` or `while let`
expressions.

The condition can use variables created in the pattern. Listing 19-26 shows a
`match` where the first arm has the pattern `Some(x)` and also has a match
Expand Down
Loading