Skip to content

Commit fd53657

Browse files
committed
auto merge of #18339 : chastell/rust/guide_pattern_fixes, r=nikomatsakis
I think it helps to show that the variables introduced in match blocks are indeed independent from the matched variable `x` (especially when `x` is still reachable inside those blocks and might be useful), so this renames them accordingly. Maybe some linter (or language-level warning?) will eventually warn about shadowing `x` in such cases. ;) I’m not super happy about the matching-on-range example, as it’s too contrived (`e` and `x` are exactly the same here), but I couldn’t come up with something both simple and non-redundant.
2 parents 52c3fe9 + aa1cd6e commit fd53657

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/doc/guide.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -3852,7 +3852,7 @@ the value to a name with `@`:
38523852
let x = 1i;
38533853
38543854
match x {
3855-
x @ 1 ... 5 => println!("got {}", x),
3855+
e @ 1 ... 5 => println!("got a range element {}", e),
38563856
_ => println!("anything"),
38573857
}
38583858
```
@@ -3885,7 +3885,7 @@ enum OptionalInt {
38853885
let x = Value(5i);
38863886
38873887
match x {
3888-
Value(x) if x > 5 => println!("Got an int bigger than five!"),
3888+
Value(i) if i > 5 => println!("Got an int bigger than five!"),
38893889
Value(..) => println!("Got an int!"),
38903890
Missing => println!("No such luck."),
38913891
}
@@ -3898,12 +3898,12 @@ with. First, `&`:
38983898
let x = &5i;
38993899
39003900
match x {
3901-
&x => println!("Got a value: {}", x),
3901+
&val => println!("Got a value: {}", val),
39023902
}
39033903
```
39043904

3905-
Here, the `x` inside the `match` has type `int`. In other words, the left hand
3906-
side of the pattern destructures the value. If we have `&5i`, then in `&x`, `x`
3905+
Here, the `val` inside the `match` has type `int`. In other words, the left hand
3906+
side of the pattern destructures the value. If we have `&5i`, then in `&val`, `val`
39073907
would be `5i`.
39083908

39093909
If you want to get a reference, use the `ref` keyword:
@@ -3912,19 +3912,19 @@ If you want to get a reference, use the `ref` keyword:
39123912
let x = 5i;
39133913
39143914
match x {
3915-
ref x => println!("Got a reference to {}", x),
3915+
ref r => println!("Got a reference to {}", r),
39163916
}
39173917
```
39183918

3919-
Here, the `x` inside the `match` has the type `&int`. In other words, the `ref`
3919+
Here, the `r` inside the `match` has the type `&int`. In other words, the `ref`
39203920
keyword _creates_ a reference, for use in the pattern. If you need a mutable
39213921
reference, `ref mut` will work in the same way:
39223922

39233923
```{rust}
39243924
let mut x = 5i;
39253925
39263926
match x {
3927-
ref mut x => println!("Got a mutable reference to {}", x),
3927+
ref mut mr => println!("Got a mutable reference to {}", mr),
39283928
}
39293929
```
39303930

0 commit comments

Comments
 (0)