From 17efd0bba0c1687627c13f8112ab10b154ff8289 Mon Sep 17 00:00:00 2001 From: Egor Suvorov Date: Tue, 5 Apr 2022 14:43:26 +0200 Subject: [PATCH 1/2] 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. --- src/ch18-03-pattern-syntax.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/ch18-03-pattern-syntax.md b/src/ch18-03-pattern-syntax.md index 903c206dda..934c37867a 100644 --- a/src/ch18-03-pattern-syntax.md +++ b/src/ch18-03-pattern-syntax.md @@ -21,9 +21,10 @@ value. 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 +named variables in conditional expressions like `match`, `if let` or `while let`. +All of them start a new scope, hence +variables declared as part of a pattern inside the conditional will +shadow those with the same name outside, as is the case with all variables. In Listing 18-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 @@ -66,10 +67,11 @@ To create a `match` expression that compares the values of the outer `x` and guard conditional instead. We’ll talk about match guards later in the [“Extra Conditionals with Match Guards”](#extra-conditionals-with-match-guards) section. +There is no such mechanism for either `if let` or `while let`. ### Multiple Patterns -In `match` expressions, you can match multiple patterns using the `|` syntax, +You can match multiple patterns using the `|` syntax, which means *or*. For example, the following code matches 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 From 01aea449d7c6fcf4417bf765f6176f0796e8dc2e Mon Sep 17 00:00:00 2001 From: Chris Krycho Date: Thu, 12 Dec 2024 10:16:22 -0700 Subject: [PATCH 2/2] 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. --- src/ch19-03-pattern-syntax.md | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/ch19-03-pattern-syntax.md b/src/ch19-03-pattern-syntax.md index ce446f476d..d685f87bf6 100644 --- a/src/ch19-03-pattern-syntax.md +++ b/src/ch19-03-pattern-syntax.md @@ -19,16 +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 conditional expressions like `match`, `if let` or `while let`. -All of them start a new scope, hence -variables declared as part of a pattern inside the conditional 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. +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. @@ -65,15 +64,13 @@ To create a `match` expression that compares the values of the outer `x` and variable, we would need to use a match guard conditional instead. We’ll talk about match guards later in the [“Extra Conditionals with Match Guards”](#extra-conditionals-with-match-guards) section. -There is no such mechanism for either `if let` or `while let`. ### Multiple Patterns -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}} @@ -454,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