Skip to content

Commit

Permalink
ch18-03: Matching Named Variables: mention if let/while let (#3110)
Browse files Browse the repository at this point in the history
* Matching Named Variables: mention `if let`/`while let`

    `if let` and `while let` introduce new scope for named variables just like `match`. However, they do not support match guards yet, see RFC 2497.

* Ch. 19: further clarify discussion of `if|while let` patterns

    - While keeping the note about `if let` and `while let` when introducing
      the discussion of matching named variables, bring the phrasing back
      closer to the original.
    - Move the distinction that `if let` and `while let` do not have the
      equivalent of match guards down to the discussion of match guards.
    - Line wrapping.

---------

Co-authored-by: Chris Krycho <[email protected]>
  • Loading branch information
yeputons and chriskrycho authored Dec 12, 2024
1 parent c9820ad commit b195aeb
Showing 1 changed file with 16 additions and 15 deletions.
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

0 comments on commit b195aeb

Please sign in to comment.