2014-12-02 08:20:48 -06:00
|
|
|
|
% Patterns
|
|
|
|
|
|
2015-04-10 11:19:26 -05:00
|
|
|
|
Patterns are quite common in Rust. We use them in [variable
|
|
|
|
|
bindings][bindings], [match statements][match], and other places, too. Let’s go
|
|
|
|
|
on a whirlwind tour of all of the things patterns can do!
|
|
|
|
|
|
|
|
|
|
[bindings]: variable-bindings.html
|
|
|
|
|
[match]: match.html
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
|
|
|
|
A quick refresher: you can match against literals directly, and `_` acts as an
|
2015-04-10 11:19:26 -05:00
|
|
|
|
‘any’ case:
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
2015-04-10 11:19:26 -05: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"),
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2015-04-18 14:52:29 -05:00
|
|
|
|
This prints `one`.
|
|
|
|
|
|
2015-04-10 11:19:26 -05:00
|
|
|
|
# Multiple patterns
|
|
|
|
|
|
2014-12-02 08:20:48 -06:00
|
|
|
|
You can match multiple patterns with `|`:
|
|
|
|
|
|
2015-04-10 11:19:26 -05:00
|
|
|
|
```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"),
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2015-04-18 14:52:29 -05:00
|
|
|
|
This prints `one or two`.
|
|
|
|
|
|
2015-04-10 11:19:26 -05:00
|
|
|
|
# Ranges
|
|
|
|
|
|
2014-12-02 08:20:48 -06:00
|
|
|
|
You can match a range of values with `...`:
|
|
|
|
|
|
2015-04-10 11:19:26 -05:00
|
|
|
|
```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"),
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2015-04-18 14:52:29 -05:00
|
|
|
|
This prints `one through five`.
|
|
|
|
|
|
|
|
|
|
Ranges are mostly used with integers and `char`s:
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
let x = '💅';
|
|
|
|
|
|
|
|
|
|
match x {
|
|
|
|
|
'a' ... 'j' => println!("early letter"),
|
|
|
|
|
'k' ... 'z' => println!("late letter"),
|
|
|
|
|
_ => println!("something else"),
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2015-05-14 13:51:16 -05:00
|
|
|
|
This prints `something else`.
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
2015-04-10 11:19:26 -05:00
|
|
|
|
# Bindings
|
|
|
|
|
|
2015-05-02 09:52:48 -05:00
|
|
|
|
You can bind values to names with `@`:
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
2015-04-10 11:19:26 -05:00
|
|
|
|
```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"),
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2015-05-02 09:52:48 -05:00
|
|
|
|
This prints `got a range element 1`. This is useful when you want to
|
|
|
|
|
do a complicated match of part of a data structure:
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
struct Person {
|
|
|
|
|
name: Option<String>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let name = "Steve".to_string();
|
|
|
|
|
let mut x: Option<Person> = Some(Person { name: Some(name) });
|
|
|
|
|
match x {
|
|
|
|
|
Some(Person { name: ref a @ Some(_), .. }) => println!("{:?}", a),
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
This prints `Some("Steve")`: We’ve bound the inner `name` to `a`.
|
|
|
|
|
|
|
|
|
|
If you use `@` with `|`, you need to make sure the name is bound in each part
|
|
|
|
|
of the pattern:
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
let x = 5;
|
|
|
|
|
|
|
|
|
|
match x {
|
|
|
|
|
e @ 1 ... 5 | e @ 8 ... 10 => println!("got a range element {}", e),
|
|
|
|
|
_ => println!("anything"),
|
|
|
|
|
}
|
|
|
|
|
```
|
2015-04-18 14:52:29 -05:00
|
|
|
|
|
2015-04-10 11:19:26 -05:00
|
|
|
|
# Ignoring variants
|
|
|
|
|
|
|
|
|
|
If you’re matching on an enum which has variants, you can use `..` to
|
2014-12-02 08:20:48 -06:00
|
|
|
|
ignore the value and type in the variant:
|
|
|
|
|
|
2015-04-10 11:19:26 -05:00
|
|
|
|
```rust
|
2014-12-02 08:20:48 -06:00
|
|
|
|
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!"),
|
2015-01-04 11:31:02 -06:00
|
|
|
|
OptionalInt::Missing => println!("No such luck."),
|
2014-12-02 08:20:48 -06:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2015-04-18 14:52:29 -05:00
|
|
|
|
This prints `Got an int!`.
|
|
|
|
|
|
2015-04-10 11:19:26 -05:00
|
|
|
|
# Guards
|
|
|
|
|
|
|
|
|
|
You can introduce ‘match guards’ with `if`:
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
2015-04-10 11:19:26 -05:00
|
|
|
|
```rust
|
2014-12-02 08:20:48 -06:00
|
|
|
|
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!"),
|
2015-01-04 11:31:02 -06:00
|
|
|
|
OptionalInt::Missing => println!("No such luck."),
|
2014-12-02 08:20:48 -06:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2015-05-14 13:51:16 -05:00
|
|
|
|
This prints `Got an int!`.
|
2015-04-18 14:52:29 -05:00
|
|
|
|
|
2015-04-10 11:19:26 -05:00
|
|
|
|
# ref and ref mut
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
2015-04-10 11:19:26 -05:00
|
|
|
|
If you want to get a [reference][ref], use the `ref` keyword:
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
2015-04-10 11:19:26 -05:00
|
|
|
|
```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-04-18 14:52:29 -05:00
|
|
|
|
This prints `Got a reference to 5`.
|
|
|
|
|
|
2015-04-10 11:19:26 -05:00
|
|
|
|
[ref]: references-and-borrowing.html
|
|
|
|
|
|
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:
|
|
|
|
|
|
2015-04-10 11:19:26 -05:00
|
|
|
|
```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),
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2015-04-10 11:19:26 -05:00
|
|
|
|
# Destructuring
|
|
|
|
|
|
2015-04-18 14:52:29 -05:00
|
|
|
|
If you have a compound data type, like a [`struct`][struct], you can destructure it
|
2015-04-10 11:19:26 -05:00
|
|
|
|
inside of a pattern:
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
2015-04-10 11:19:26 -05:00
|
|
|
|
```rust
|
2014-12-02 08:20:48 -06:00
|
|
|
|
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),
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2015-04-18 14:52:29 -05:00
|
|
|
|
[struct]: structs.html
|
|
|
|
|
|
2015-04-10 11:19:26 -05:00
|
|
|
|
If we only care about some of the values, we don’t have to give them all names:
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
2015-04-10 11:19:26 -05:00
|
|
|
|
```rust
|
2014-12-02 08:20:48 -06:00
|
|
|
|
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),
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2015-04-18 14:52:29 -05:00
|
|
|
|
This prints `x is 0`.
|
|
|
|
|
|
2014-12-02 08:20:48 -06:00
|
|
|
|
You can do this kind of match on any member, not just the first:
|
|
|
|
|
|
2015-04-10 11:19:26 -05:00
|
|
|
|
```rust
|
2014-12-02 08:20:48 -06:00
|
|
|
|
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),
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2015-04-18 14:52:29 -05:00
|
|
|
|
This prints `y is 0`.
|
|
|
|
|
|
2015-04-10 11:19:26 -05:00
|
|
|
|
This ‘destructuring’ behavior works on any compound data type, like
|
|
|
|
|
[tuples][tuples] or [enums][enums].
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
2015-04-10 11:19:26 -05:00
|
|
|
|
[tuples]: primitive-types.html#tuples
|
|
|
|
|
[enums]: enums.html
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
2015-04-10 11:19:26 -05:00
|
|
|
|
# Mix and Match
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
2015-04-10 11:19:26 -05:00
|
|
|
|
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:
|
2014-12-02 08:20:48 -06:00
|
|
|
|
|
2015-04-18 14:52:29 -05:00
|
|
|
|
```rust,ignore
|
2014-12-02 08:20:48 -06:00
|
|
|
|
match x {
|
|
|
|
|
Foo { x: Some(ref name), y: None } => ...
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2015-04-18 14:52:29 -05:00
|
|
|
|
Patterns are very powerful. Make good use of them.
|