@@ -2865,7 +2865,7 @@ match_pat : pat [ ".." pat ] ? [ "if" expr ] ;
2865
2865
A ` match ` expression branches on a * pattern* . The exact form of matching that
2866
2866
occurs depends on the pattern. Patterns consist of some combination of
2867
2867
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
2869
2869
expression* , which is the value to compare to the patterns. The type of the
2870
2870
patterns must equal the type of the head expression.
2871
2871
@@ -2887,7 +2887,7 @@ match x {
2887
2887
2888
2888
The first pattern matches lists constructed by applying ` Cons ` to any head value, and a
2889
2889
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
2891
2891
` C(_) ` is only type-correct if ` C ` has exactly one argument, while the pattern ` C(..) ` is
2892
2892
type-correct for any enum variant ` C ` , regardless of how many arguments ` C ` has.
2893
2893
@@ -2939,6 +2939,27 @@ This can be changed to bind to a reference by
2939
2939
using the ` ref ` keyword,
2940
2940
or to a mutable reference using ` ref mut ` .
2941
2941
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
+
2942
2963
Patterns can also dereference pointers by using the ` & ` ,
2943
2964
` ~ ` or ` @ ` symbols, as appropriate. For example, these two matches
2944
2965
on ` x: &int ` are equivalent:
0 commit comments