@@ -2893,12 +2893,21 @@ tail value of `~Nil`. The second pattern matches _any_ list constructed with `Co
2893
2893
ignoring the values of its arguments. The difference between ` _ ` and ` * ` is that the pattern ` C(_) ` is only type-correct if
2894
2894
` C ` has exactly one argument, while the pattern ` C(..) ` is type-correct for any enum variant ` C ` , regardless of how many arguments ` C ` has.
2895
2895
2896
- To execute an ` match ` expression, first the head expression is evaluated, then
2897
- its value is sequentially compared to the patterns in the arms until a match
2896
+ A ` match ` behaves differently depending on whether or not the head expression
2897
+ is an [ lvalue or an rvalue] ( #lvalues-rvalues-and-temporaries ) .
2898
+ If the head expression is an rvalue, it is
2899
+ first evaluated into a temporary location, and the resulting value
2900
+ is sequentially compared to the patterns in the arms until a match
2898
2901
is found. The first arm with a matching pattern is chosen as the branch target
2899
2902
of the ` match ` , any variables bound by the pattern are assigned to local
2900
2903
variables in the arm's block, and control enters the block.
2901
2904
2905
+ When the head expression is an lvalue, the match does not allocate a
2906
+ temporary location (however, a by-value binding may copy or move from
2907
+ the lvalue). When possible, it is preferable to match on lvalues, as the
2908
+ lifetime of these matches inherits the lifetime of the lvalue, rather
2909
+ than being restricted to the inside of the match.
2910
+
2902
2911
An example of an ` match ` expression:
2903
2912
2904
2913
~~~~
@@ -2932,6 +2941,15 @@ This can be changed to bind to a reference by
2932
2941
using the ``` ref ``` keyword,
2933
2942
or to a mutable reference using ``` ref mut ``` .
2934
2943
2944
+ Patterns can also dereference pointers by using the `` & `` ,
2945
+ `` ~ `` or `` @ `` symbols, as appropriate. For example, these two matches
2946
+ on `` x: &int `` are equivalent:
2947
+
2948
+ ~~~~
2949
+ match *x { 0 => "zero", _ => "some" }
2950
+ match x { &0 => "zero", _ => "some" }
2951
+ ~~~~
2952
+
2935
2953
A pattern that's just an identifier,
2936
2954
like ` Nil ` in the previous answer,
2937
2955
could either refer to an enum variant that's in scope,
0 commit comments