@@ -123,13 +123,15 @@ comma:
123
123
124
124
## Struct expressions
125
125
126
- There are several forms of struct expressions. A _ struct expression_
127
- consists of the [ path] ( paths.html ) of a [ struct item] ( items.html#structs ) , followed
128
- by a brace-enclosed list of zero or more comma-separated name-value pairs,
129
- providing the field values of a new instance of the struct. A field name can be
130
- any identifier, and is separated from its value expression by a colon. The
131
- location denoted by a struct field is mutable if and only if the enclosing
132
- struct is mutable.
126
+ There are several forms of struct expressions. A _ struct expression_ consists
127
+ of the [ path] ( paths.html ) of a [ struct item] ( items.html#structs ) , followed by a
128
+ brace-enclosed list of zero or more comma-separated name-value pairs, providing
129
+ the field values of a new instance of the struct. A field name can be any
130
+ identifier, and is separated from its value expression by a colon. In the case
131
+ of a tuple struct the field names are numbers corresponding to the position of
132
+ the field. The numbers must be written in decimal, containing no underscores
133
+ and with no leading zeros or integer suffix. The location denoted by a struct
134
+ field is mutable if and only if the enclosing struct is mutable.
133
135
134
136
A _ tuple struct expression_ consists of the [ path] ( paths.html ) of a [ struct
135
137
item] ( items.html#structs ) , followed by a parenthesized list of one or more
@@ -150,6 +152,7 @@ The following are examples of struct expressions:
150
152
Point {x : 10.0 , y : 20.0 };
151
153
NothingInMe {};
152
154
TuplePoint (10.0 , 20.0 );
155
+ TuplePoint { 0 : 10.0 , 1 : 20.0 }; // Results in the same value as the above line
153
156
let u = game :: User {name : " Joe" , age : 35 , score : 100_000 };
154
157
some_fn :: <Cookie >(Cookie );
155
158
```
@@ -174,9 +177,9 @@ Point3d {y: 0, z: 10, .. base};
174
177
175
178
#### Struct field init shorthand
176
179
177
- When initializing a data structure (struct, enum, union) with named fields,
178
- it is allowed to write ` fieldname ` as a shorthand for ` fieldname: fieldname ` .
179
- This allows a compact syntax with less duplication.
180
+ When initializing a data structure (struct, enum, union) with named (but not
181
+ numbered) fields, it is allowed to write ` fieldname ` as a shorthand for
182
+ ` fieldname: fieldname ` . This allows a compact syntax with less duplication.
180
183
181
184
Example:
182
185
@@ -698,10 +701,6 @@ variable binding specifications, wildcards (`..`), and placeholders (`_`). A
698
701
the patterns. The type of the patterns must equal the type of the head
699
702
expression.
700
703
701
- In a pattern whose head expression has an ` enum ` type, a placeholder (` _ ` )
702
- stands for a * single* data field, whereas a wildcard ` .. ` stands for * all* the
703
- fields of a particular variant.
704
-
705
704
A ` match ` behaves differently depending on whether or not the head expression
706
705
is an [ lvalue or an rvalue] ( expressions.html#lvalues-rvalues-and-temporaries ) .
707
706
If the head expression is an rvalue, it is first evaluated into a temporary
@@ -737,16 +736,31 @@ matched value (depending on the matched value's type). This can be changed to
737
736
bind to a reference by using the ` ref ` keyword, or to a mutable reference using
738
737
` ref mut ` .
739
738
740
- Subpatterns can also be bound to variables by the use of the syntax `variable @
741
- subpattern`. For example:
739
+ Patterns can be used to * destructure* structs, enums, and tuples. Destructuring
740
+ breaks a value up into its component pieces. The syntax used is the same as
741
+ when creating such values. When destructing a data structure with named (but
742
+ not numbered) fields, it is allowed to write ` fieldname ` as a shorthand for
743
+ ` fieldname: fieldname ` . In a pattern whose head expression has a ` struct ` ,
744
+ ` enum ` or ` tupl ` type, a placeholder (` _ ` ) stands for a * single* data field,
745
+ whereas a wildcard ` .. ` stands for * all* the fields of a particular variant.
742
746
743
747
``` rust
744
- let x = 1 ;
745
-
746
- match x {
747
- e @ 1 ... 5 => println! (" got a range element {}" , e ),
748
- _ => println! (" anything" ),
749
- }
748
+ # enum Message {
749
+ # Quit ,
750
+ # WriteString (String ),
751
+ # Move { x : i32 , y : i32 },
752
+ # ChangeColor (u8 , u8 , u8 ),
753
+ # }
754
+ # let message = Message :: Quit ;
755
+ match message {
756
+ Message :: Quit => println! (" Quit" ),
757
+ Message :: WriteString (write ) => println! (" {}" , & write ),
758
+ Message :: Move { x , y : 0 } => println! (" move {} horizontally" , x ),
759
+ Message :: Move { .. } => println! (" other move" ),
760
+ Message :: ChangeColor { 0 : red , 1 : green , 2 : _ } => {
761
+ println! (" color change, red: {}, green: {}" , red , green );
762
+ }
763
+ };
750
764
```
751
765
752
766
Patterns can also dereference pointers by using the ` & ` , ` &mut ` and ` box `
@@ -761,6 +775,18 @@ let z = match x { &0 => "zero", _ => "some" };
761
775
assert_eq! (y , z );
762
776
```
763
777
778
+ Subpatterns can also be bound to variables by the use of the syntax `variable @
779
+ subpattern`. For example:
780
+
781
+ ``` rust
782
+ let x = 1 ;
783
+
784
+ match x {
785
+ e @ 1 ... 5 => println! (" got a range element {}" , e ),
786
+ _ => println! (" anything" ),
787
+ }
788
+ ```
789
+
764
790
Multiple match patterns may be joined with the ` | ` operator. A range of values
765
791
may be specified with ` ... ` . For example:
766
792
0 commit comments