Skip to content

Commit ee3f074

Browse files
committed
Guide: patterns.
Fixes #4417.
1 parent c7d0b52 commit ee3f074

File tree

1 file changed

+167
-1
lines changed

1 file changed

+167
-1
lines changed

src/doc/guide.md

+167-1
Original file line numberDiff line numberDiff line change
@@ -3759,6 +3759,173 @@ guide](http://doc.rust-lang.org/guide-pointers.html#rc-and-arc).
37593759

37603760
# Patterns
37613761

3762+
We've made use of patterns a few times in the guide: first with `let` bindings,
3763+
then with `match` statements. Let's go on a whirlwind tour of all of the things
3764+
patterns can do!
3765+
3766+
A quick refresher: you can match against literals directly, and `_` acts as an
3767+
'any' case:
3768+
3769+
```{rust}
3770+
let x = 1i;
3771+
3772+
match x {
3773+
1 => println!("one"),
3774+
2 => println!("two"),
3775+
3 => println!("three"),
3776+
_ => println!("anything"),
3777+
}
3778+
```
3779+
3780+
You can match multiple patterns with `|`:
3781+
3782+
```{rust}
3783+
let x = 1i;
3784+
3785+
match x {
3786+
1 | 2 => println!("one or two"),
3787+
3 => println!("three"),
3788+
_ => println!("anything"),
3789+
}
3790+
```
3791+
3792+
You can match a range of values with `..`:
3793+
3794+
```{rust}
3795+
let x = 1i;
3796+
3797+
match x {
3798+
1 .. 5 => println!("one through five"),
3799+
_ => println!("anything"),
3800+
}
3801+
```
3802+
3803+
Ranges are mostly used with integers and single characters.
3804+
3805+
If you're matching multiple things, via a `|` or a `..`, you can bind
3806+
the value to a name with `@`:
3807+
3808+
```{rust}
3809+
let x = 1i;
3810+
3811+
match x {
3812+
x @ 1 .. 5 => println!("got {}", x),
3813+
_ => println!("anything"),
3814+
}
3815+
```
3816+
3817+
If you're matching on an enum which has variants, you can use `..` to
3818+
ignore the value in the variant:
3819+
3820+
```{rust}
3821+
enum OptionalInt {
3822+
Value(int),
3823+
Missing,
3824+
}
3825+
3826+
let x = Value(5i);
3827+
3828+
match x {
3829+
Value(..) => println!("Got an int!"),
3830+
Missing => println!("No such luck."),
3831+
}
3832+
```
3833+
3834+
You can introduce **match guards** with `if`:
3835+
3836+
```{rust}
3837+
enum OptionalInt {
3838+
Value(int),
3839+
Missing,
3840+
}
3841+
3842+
let x = Value(5i);
3843+
3844+
match x {
3845+
Value(x) if x > 5 => println!("Got an int bigger than five!"),
3846+
Value(..) => println!("Got an int!"),
3847+
Missing => println!("No such luck."),
3848+
}
3849+
```
3850+
3851+
If you're matching on a pointer, you can use the same syntax as you declared it
3852+
with. First, `&`:
3853+
3854+
```{rust}
3855+
let x = &5i;
3856+
3857+
match x {
3858+
&x => println!("Got a value: {}", x),
3859+
}
3860+
```
3861+
3862+
Here, the `x` inside the `match` has type `int`. In other words, the left hand
3863+
side of the pattern destructures the value. If we have `&5i`, then in `&x`, `x`
3864+
would be `5i`.
3865+
3866+
If you want to get a reference, use the `ref` keyword:
3867+
3868+
```{rust}
3869+
let x = 5i;
3870+
3871+
match x {
3872+
ref x => println!("Got a reference to {}", x),
3873+
}
3874+
```
3875+
3876+
Here, the `x` inside the `match` has the type `&int`. In other words, the `ref`
3877+
keyword _creates_ a reference, for use in the pattern. If you need a mutable
3878+
reference, `ref mut` will work in the same way:
3879+
3880+
```{rust}
3881+
let mut x = 5i;
3882+
3883+
match x {
3884+
ref mut x => println!("Got a mutable reference to {}", x),
3885+
}
3886+
```
3887+
3888+
If you have a struct, you can desugar it inside of a pattern:
3889+
3890+
```{rust}
3891+
struct Point {
3892+
x: int,
3893+
y: int,
3894+
}
3895+
3896+
let origin = Point { x: 0i, y: 0i };
3897+
3898+
match origin {
3899+
Point { x: x, y: y } => println!("({},{})", x, y),
3900+
}
3901+
```
3902+
3903+
If we only care about some of the values, we don't have to give them all names:
3904+
3905+
```{rust}
3906+
struct Point {
3907+
x: int,
3908+
y: int,
3909+
}
3910+
3911+
let origin = Point { x: 0i, y: 0i };
3912+
3913+
match origin {
3914+
Point { x: x, .. } => println!("x is {}", x),
3915+
}
3916+
```
3917+
3918+
Whew! That's a lot of different ways to match things, and they can all be
3919+
mixed and matched, depending on what you're doing:
3920+
3921+
```{rust,ignore}
3922+
match x {
3923+
Foo { x: Some(ref name), y: None } => ...
3924+
}
3925+
```
3926+
3927+
Patterns are very powerful. Make good use of them.
3928+
37623929
# Method Syntax
37633930

37643931
Functions are great, but if you want to call a bunch of them on some data, it
@@ -3846,7 +4013,6 @@ This **static method** builds a new `Circle` for us. Note that static methods
38464013
are called with the `Struct::method()` syntax, rather than the `ref.method()`
38474014
syntax.
38484015

3849-
38504016
# Closures
38514017

38524018
So far, we've made lots of functions in Rust. But we've given them all names.

0 commit comments

Comments
 (0)