Skip to content

Commit

Permalink
Guide: Patterns: use non-x variables in match blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
chastell committed Oct 26, 2014
1 parent f168c12 commit aa1cd6e
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/doc/guide.md
Expand Up @@ -3801,7 +3801,7 @@ the value to a name with `@`:
let x = 1i;
match x {
x @ 1 ... 5 => println!("got {}", x),
e @ 1 ... 5 => println!("got a range element {}", e),
_ => println!("anything"),
}
```
Expand Down Expand Up @@ -3834,7 +3834,7 @@ enum OptionalInt {
let x = Value(5i);
match x {
Value(x) if x > 5 => println!("Got an int bigger than five!"),
Value(i) if i > 5 => println!("Got an int bigger than five!"),
Value(..) => println!("Got an int!"),
Missing => println!("No such luck."),
}
Expand All @@ -3847,12 +3847,12 @@ with. First, `&`:
let x = &5i;
match x {
&x => println!("Got a value: {}", x),
&val => println!("Got a value: {}", val),
}
```

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

If you want to get a reference, use the `ref` keyword:
Expand All @@ -3861,19 +3861,19 @@ If you want to get a reference, use the `ref` keyword:
let x = 5i;
match x {
ref x => println!("Got a reference to {}", x),
ref r => println!("Got a reference to {}", r),
}
```

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

```{rust}
let mut x = 5i;
match x {
ref mut x => println!("Got a mutable reference to {}", x),
ref mut mr => println!("Got a mutable reference to {}", mr),
}
```

Expand Down

5 comments on commit aa1cd6e

@bors
Copy link
Contributor

@bors bors commented on aa1cd6e Oct 30, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from nikomatsakis
at chastell@aa1cd6e

@bors
Copy link
Contributor

@bors bors commented on aa1cd6e Oct 30, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging chastell/rust/guide_pattern_fixes = aa1cd6e into auto

@bors
Copy link
Contributor

@bors bors commented on aa1cd6e Oct 30, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chastell/rust/guide_pattern_fixes = aa1cd6e merged ok, testing candidate = fd53657

@bors
Copy link
Contributor

@bors bors commented on aa1cd6e Oct 30, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on aa1cd6e Oct 30, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = fd53657

Please sign in to comment.