2014-12-02 08:20:48 -06:00
|
|
|
% Patterns
|
|
|
|
|
|
|
|
We've made use of patterns a few times in the guide: first with `let` bindings,
|
|
|
|
then with `match` statements. Let's go on a whirlwind tour of all of the things
|
|
|
|
patterns can do!
|
|
|
|
|
|
|
|
A quick refresher: you can match against literals directly, and `_` acts as an
|
2015-01-08 18:52:50 -06:00
|
|
|
*any* case:
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
|
|
```{rust}
|
2015-01-13 09:40:18 -06:00
|
|
|
let x = 1;
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
|
|
match x {
|
|
|
|
1 => println!("one"),
|
|
|
|
2 => println!("two"),
|
|
|
|
3 => println!("three"),
|
|
|
|
_ => println!("anything"),
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
You can match multiple patterns with `|`:
|
|
|
|
|
|
|
|
```{rust}
|
2015-01-13 09:40:18 -06:00
|
|
|
let x = 1;
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
|
|
match x {
|
|
|
|
1 | 2 => println!("one or two"),
|
|
|
|
3 => println!("three"),
|
|
|
|
_ => println!("anything"),
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
You can match a range of values with `...`:
|
|
|
|
|
|
|
|
```{rust}
|
2015-01-13 09:40:18 -06:00
|
|
|
let x = 1;
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
|
|
match x {
|
|
|
|
1 ... 5 => println!("one through five"),
|
|
|
|
_ => println!("anything"),
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Ranges are mostly used with integers and single characters.
|
|
|
|
|
|
|
|
If you're matching multiple things, via a `|` or a `...`, you can bind
|
|
|
|
the value to a name with `@`:
|
|
|
|
|
|
|
|
```{rust}
|
2015-01-13 09:40:18 -06:00
|
|
|
let x = 1;
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
|
|
match x {
|
|
|
|
e @ 1 ... 5 => println!("got a range element {}", e),
|
|
|
|
_ => println!("anything"),
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
If you're matching on an enum which has variants, you can use `..` to
|
|
|
|
ignore the value and type in the variant:
|
|
|
|
|
|
|
|
```{rust}
|
|
|
|
enum OptionalInt {
|
2015-01-13 09:40:18 -06:00
|
|
|
Value(i32),
|
2014-12-02 08:20:48 -06:00
|
|
|
Missing,
|
|
|
|
}
|
|
|
|
|
2015-01-13 09:40:18 -06:00
|
|
|
let x = OptionalInt::Value(5);
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
|
|
match x {
|
|
|
|
OptionalInt::Value(..) => println!("Got an int!"),
|
|
|
|
OptionalInt::Missing => println!("No such luck."),
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2015-01-08 18:52:50 -06:00
|
|
|
You can introduce *match guards* with `if`:
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
|
|
```{rust}
|
|
|
|
enum OptionalInt {
|
2015-01-13 09:40:18 -06:00
|
|
|
Value(i32),
|
2014-12-02 08:20:48 -06:00
|
|
|
Missing,
|
|
|
|
}
|
|
|
|
|
2015-01-13 09:40:18 -06:00
|
|
|
let x = OptionalInt::Value(5);
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
|
|
match x {
|
|
|
|
OptionalInt::Value(i) if i > 5 => println!("Got an int bigger than five!"),
|
|
|
|
OptionalInt::Value(..) => println!("Got an int!"),
|
|
|
|
OptionalInt::Missing => println!("No such luck."),
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
If you're matching on a pointer, you can use the same syntax as you declared it
|
|
|
|
with. First, `&`:
|
|
|
|
|
|
|
|
```{rust}
|
2015-01-13 09:40:18 -06:00
|
|
|
let x = &5;
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
|
|
match x {
|
|
|
|
&val => println!("Got a value: {}", val),
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2015-01-13 09:40:18 -06:00
|
|
|
Here, the `val` inside the `match` has type `i32`. In other words, the left-hand
|
|
|
|
side of the pattern destructures the value. If we have `&5`, then in `&val`, `val`
|
|
|
|
would be `5`.
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
|
|
If you want to get a reference, use the `ref` keyword:
|
|
|
|
|
|
|
|
```{rust}
|
2015-01-13 09:40:18 -06:00
|
|
|
let x = 5;
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
|
|
match x {
|
|
|
|
ref r => println!("Got a reference to {}", r),
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2015-01-13 09:40:18 -06:00
|
|
|
Here, the `r` inside the `match` has the type `&i32`. In other words, the `ref`
|
2014-12-02 08:20:48 -06:00
|
|
|
keyword _creates_ a reference, for use in the pattern. If you need a mutable
|
|
|
|
reference, `ref mut` will work in the same way:
|
|
|
|
|
|
|
|
```{rust}
|
2015-01-13 09:40:18 -06:00
|
|
|
let mut x = 5;
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
|
|
match x {
|
|
|
|
ref mut mr => println!("Got a mutable reference to {}", mr),
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
If you have a struct, you can destructure it inside of a pattern:
|
|
|
|
|
|
|
|
```{rust}
|
|
|
|
# #![allow(non_shorthand_field_patterns)]
|
|
|
|
struct Point {
|
2015-01-13 09:40:18 -06:00
|
|
|
x: i32,
|
|
|
|
y: i32,
|
2014-12-02 08:20:48 -06:00
|
|
|
}
|
|
|
|
|
2015-01-13 09:40:18 -06:00
|
|
|
let origin = Point { x: 0, y: 0 };
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
|
|
match origin {
|
|
|
|
Point { x: x, y: y } => println!("({},{})", x, y),
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
If we only care about some of the values, we don't have to give them all names:
|
|
|
|
|
|
|
|
```{rust}
|
|
|
|
# #![allow(non_shorthand_field_patterns)]
|
|
|
|
struct Point {
|
2015-01-13 09:40:18 -06:00
|
|
|
x: i32,
|
|
|
|
y: i32,
|
2014-12-02 08:20:48 -06:00
|
|
|
}
|
|
|
|
|
2015-01-13 09:40:18 -06:00
|
|
|
let origin = Point { x: 0, y: 0 };
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
|
|
match origin {
|
|
|
|
Point { x: x, .. } => println!("x is {}", x),
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
You can do this kind of match on any member, not just the first:
|
|
|
|
|
|
|
|
```{rust}
|
|
|
|
# #![allow(non_shorthand_field_patterns)]
|
|
|
|
struct Point {
|
2015-01-13 09:40:18 -06:00
|
|
|
x: i32,
|
|
|
|
y: i32,
|
2014-12-02 08:20:48 -06:00
|
|
|
}
|
|
|
|
|
2015-01-13 09:40:18 -06:00
|
|
|
let origin = Point { x: 0, y: 0 };
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
|
|
match origin {
|
|
|
|
Point { y: y, .. } => println!("y is {}", y),
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
If you want to match against a slice or array, you can use `[]`:
|
|
|
|
|
|
|
|
```{rust}
|
|
|
|
fn main() {
|
|
|
|
let v = vec!["match_this", "1"];
|
|
|
|
|
|
|
|
match v.as_slice() {
|
|
|
|
["match_this", second] => println!("The second element is {}", second),
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Whew! That's a lot of different ways to match things, and they can all be
|
|
|
|
mixed and matched, depending on what you're doing:
|
|
|
|
|
|
|
|
```{rust,ignore}
|
|
|
|
match x {
|
|
|
|
Foo { x: Some(ref name), y: None } => ...
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Patterns are very powerful. Make good use of them.
|