Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 72e8889

Browse files
committedJan 25, 2014
auto merge of #11764 : Armavica/rust/doc_patmatch, r=pcwalton
I also removed the obsolete '*' wildcard from the manual.
2 parents dc48adc + 7b1d124 commit 72e8889

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed
 

‎doc/rust.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2865,7 +2865,7 @@ match_pat : pat [ ".." pat ] ? [ "if" expr ] ;
28652865
A `match` expression branches on a *pattern*. The exact form of matching that
28662866
occurs depends on the pattern. Patterns consist of some combination of
28672867
literals, destructured enum constructors, structures, records and tuples, variable binding
2868-
specifications, wildcards (`*`), and placeholders (`_`). A `match` expression has a *head
2868+
specifications, wildcards (`..`), and placeholders (`_`). A `match` expression has a *head
28692869
expression*, which is the value to compare to the patterns. The type of the
28702870
patterns must equal the type of the head expression.
28712871

@@ -2887,7 +2887,7 @@ match x {
28872887

28882888
The first pattern matches lists constructed by applying `Cons` to any head value, and a
28892889
tail value of `~Nil`. The second pattern matches _any_ list constructed with `Cons`,
2890-
ignoring the values of its arguments. The difference between `_` and `*` is that the pattern
2890+
ignoring the values of its arguments. The difference between `_` and `..` is that the pattern
28912891
`C(_)` is only type-correct if `C` has exactly one argument, while the pattern `C(..)` is
28922892
type-correct for any enum variant `C`, regardless of how many arguments `C` has.
28932893

@@ -2939,6 +2939,27 @@ This can be changed to bind to a reference by
29392939
using the `ref` keyword,
29402940
or to a mutable reference using `ref mut`.
29412941

2942+
Subpatterns can also be bound to variables by the use of the syntax
2943+
`variable @ pattern`.
2944+
For example:
2945+
2946+
~~~~
2947+
enum List { Nil, Cons(uint, ~List) }
2948+
2949+
fn is_sorted(list: &List) -> bool {
2950+
match *list {
2951+
Nil | Cons(_, ~Nil) => true,
2952+
Cons(x, ref r @ ~Cons(y, _)) => (x <= y) && is_sorted(*r)
2953+
}
2954+
}
2955+
2956+
fn main() {
2957+
let a = Cons(6, ~Cons(7, ~Cons(42, ~Nil)));
2958+
assert!(is_sorted(&a));
2959+
}
2960+
2961+
~~~~
2962+
29422963
Patterns can also dereference pointers by using the `&`,
29432964
`~` or `@` symbols, as appropriate. For example, these two matches
29442965
on `x: &int` are equivalent:

‎doc/tutorial.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,16 @@ to the value of the matched value inside of the arm's action. Thus, `(0.0,
520520
y)` matches any tuple whose first element is zero, and binds `y` to
521521
the second element. `(x, y)` matches any two-element tuple, and binds both
522522
elements to variables.
523+
A subpattern can also be bound to a variable, using `variable @ pattern`. For
524+
example:
525+
526+
~~~~
527+
# let age = 23;
528+
match age {
529+
a @ 0..20 => println!("{} years old", a),
530+
_ => println!("older than 21")
531+
}
532+
~~~~
523533

524534
Any `match` arm can have a guard clause (written `if EXPR`), called a
525535
*pattern guard*, which is an expression of type `bool` that

0 commit comments

Comments
 (0)
Please sign in to comment.