Skip to content

Commit 810d8ed

Browse files
authored
Merge pull request #4121 from rust-lang/error-fixes-shadowing
Fix a couple inverted uses of “shadowed”
2 parents e16fc30 + e945e17 commit 810d8ed

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

src/ch12-05-working-with-environment-variables.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ they’ll be the same case when we check whether the line contains the query.
5555

5656
</Listing>
5757

58-
First we lowercase the `query` string and store it in a shadowed variable with
59-
the same name. Calling `to_lowercase` on the query is necessary so that no
60-
matter whether the user’s query is `"rust"`, `"RUST"`, `"Rust"`, or `"rUsT"`,
61-
we’ll treat the query as if it were `"rust"` and be insensitive to the case.
62-
While `to_lowercase` will handle basic Unicode, it won’t be 100% accurate. If
63-
we were writing a real application, we’d want to do a bit more work here, but
64-
this section is about environment variables, not Unicode, so we’ll leave it at
65-
that here.
58+
First we lowercase the `query` string and store it in a new variable with the
59+
same name, shadowing the original. Calling `to_lowercase` on the query is
60+
necessary so that no matter whether the user’s query is `"rust"`, `"RUST"`,
61+
`"Rust"`, or `"rUsT"`, we’ll treat the query as if it were `"rust"` and be
62+
insensitive to the case. While `to_lowercase` will handle basic Unicode, it
63+
won’t be 100% accurate. If we were writing a real application, we’d want to do a
64+
bit more work here, but this section is about environment variables, not
65+
Unicode, so we’ll leave it at that here.
6666

6767
Note that `query` is now a `String` rather than a string slice because calling
6868
`to_lowercase` creates new data rather than referencing existing data. Say the

src/ch19-01-all-the-places-for-patterns.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ This conditional structure lets us support complex requirements. With the
8282
hardcoded values we have here, this example will print `Using purple as the
8383
background color`.
8484

85-
You can see that `if let` can also introduce shadowed variables in the same way
86-
that `match` arms can: the line `if let Ok(age) = age` introduces a new
87-
shadowed `age` variable that contains the value inside the `Ok` variant. This
88-
means we need to place the `if age > 30` condition within that block: we can’t
89-
combine these two conditions into `if let Ok(age) = age && age > 30`. The
90-
shadowed `age` we want to compare to 30 isn’t valid until the new scope starts
91-
with the curly bracket.
85+
You can see that `if let` can also introduce new variables which shadow existing
86+
variables in the same way that `match` arms can: the line `if let Ok(age) = age`
87+
introduces a new shadowed `age` variable that contains the value inside the `Ok`
88+
variant. This means we need to place the `if age > 30` condition within that
89+
block: we can’t combine these two conditions into `if let Ok(age) = age && age >
90+
30`. The shadowed `age` we want to compare to 30 isn’t valid until the new scope
91+
starts with the curly bracket.
9292

9393
The downside of using `if let` expressions is that the compiler doesn’t check
9494
for exhaustiveness, whereas with `match` expressions it does. If we omitted the

src/ch19-03-pattern-syntax.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ value `Some(5)` and a variable `y` with the value `10`. We then create a
2929
`println!` at the end, and try to figure out what the code will print before
3030
running this code or reading further.
3131

32-
<Listing number="19-11" file-name="src/main.rs" caption="A `match` expression with an arm that introduces a shadowed variable `y`">
32+
<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`">
3333

3434
```rust
3535
{{#rustdoc_include ../listings/ch19-patterns-and-matching/listing-19-11/src/main.rs:here}}
@@ -60,10 +60,10 @@ When the `match` expression is done, its scope ends, and so does the scope of
6060
the inner `y`. The last `println!` produces `at the end: x = Some(5), y = 10`.
6161

6262
To create a `match` expression that compares the values of the outer `x` and
63-
`y`, rather than introducing a shadowed variable, we would need to use a match
64-
guard conditional instead. We’ll talk about match guards later in the [“Extra
65-
Conditionals with Match Guards”](#extra-conditionals-with-match-guards)<!--
66-
ignore --> section.
63+
`y`, rather than introducing a new variable which shadows the existing `y`
64+
variable, we would need to use a match guard conditional instead. We’ll talk
65+
about match guards later in the [“Extra Conditionals with Match
66+
Guards”](#extra-conditionals-with-match-guards)<!-- ignore --> section.
6767

6868
### Multiple Patterns
6969

@@ -503,8 +503,8 @@ pattern as `Some(y)`, which would have shadowed the outer `y`, we specify
503503
`Some(n)`. This creates a new variable `n` that doesn’t shadow anything because
504504
there is no `n` variable outside the `match`.
505505

506-
The match guard `if n == y` is not a pattern and therefore doesn’t introduce
507-
new variables. This `y` *is* the outer `y` rather than a new shadowed `y`, and
506+
The match guard `if n == y` is not a pattern and therefore doesn’t introduce new
507+
variables. This `y` *is* the outer `y` rather than a new `y` shadowing it, and
508508
we can look for a value that has the same value as the outer `y` by comparing
509509
`n` to `y`.
510510

0 commit comments

Comments
 (0)