Skip to content

Commit e11cb5b

Browse files
committed
auto merge of #16257 : steveklabnik/rust/guide_patterns, r=brson
Fixes #4417.
2 parents 655600b + ee3f074 commit e11cb5b

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
@@ -3771,6 +3771,173 @@ guide](http://doc.rust-lang.org/guide-pointers.html#rc-and-arc).
37713771

37723772
# Patterns
37733773

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

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

3861-
38624028
# Closures
38634029

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

0 commit comments

Comments
 (0)