Skip to content

Commit aa1cd6e

Browse files
committed
Guide: Patterns: use non-x variables in match blocks
1 parent f168c12 commit aa1cd6e

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/doc/guide.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3801,7 +3801,7 @@ the value to a name with `@`:
38013801
let x = 1i;
38023802
38033803
match x {
3804-
x @ 1 ... 5 => println!("got {}", x),
3804+
e @ 1 ... 5 => println!("got a range element {}", e),
38053805
_ => println!("anything"),
38063806
}
38073807
```
@@ -3834,7 +3834,7 @@ enum OptionalInt {
38343834
let x = Value(5i);
38353835
38363836
match x {
3837-
Value(x) if x > 5 => println!("Got an int bigger than five!"),
3837+
Value(i) if i > 5 => println!("Got an int bigger than five!"),
38383838
Value(..) => println!("Got an int!"),
38393839
Missing => println!("No such luck."),
38403840
}
@@ -3847,12 +3847,12 @@ with. First, `&`:
38473847
let x = &5i;
38483848
38493849
match x {
3850-
&x => println!("Got a value: {}", x),
3850+
&val => println!("Got a value: {}", val),
38513851
}
38523852
```
38533853

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

38583858
If you want to get a reference, use the `ref` keyword:
@@ -3861,19 +3861,19 @@ If you want to get a reference, use the `ref` keyword:
38613861
let x = 5i;
38623862
38633863
match x {
3864-
ref x => println!("Got a reference to {}", x),
3864+
ref r => println!("Got a reference to {}", r),
38653865
}
38663866
```
38673867

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

38723872
```{rust}
38733873
let mut x = 5i;
38743874
38753875
match x {
3876-
ref mut x => println!("Got a mutable reference to {}", x),
3876+
ref mut mr => println!("Got a mutable reference to {}", mr),
38773877
}
38783878
```
38793879

0 commit comments

Comments
 (0)