@@ -123,13 +123,15 @@ comma:
123123
124124## Struct expressions
125125
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 structs the field names are instead decimal integer literals,
132+ containing no underscores and with no leading zeros or suffix, corresponding to
133+ the position of the field. The location denoted by a struct field is mutable if
134+ and only if the enclosing struct is mutable.
133135
134136A _ tuple struct expression_ consists of the [ path] ( paths.html ) of a [ struct
135137item] ( items.html#structs ) , followed by a parenthesized list of one or more
@@ -150,6 +152,7 @@ The following are examples of struct expressions:
150152Point {x : 10.0 , y : 20.0 };
151153NothingInMe {};
152154TuplePoint (10.0 , 20.0 );
155+ TuplePoint { 0 : 10.0 , 1 : 20.0 }; // Same as above line
153156let u = game :: User {name : " Joe" , age : 35 , score : 100_000 };
154157some_fn :: <Cookie >(Cookie );
155158```
@@ -174,9 +177,9 @@ Point3d {y: 0, z: 10, .. base};
174177
175178#### Struct field init shorthand
176179
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.
180183
181184Example:
182185
@@ -698,10 +701,6 @@ variable binding specifications, wildcards (`..`), and placeholders (`_`). A
698701the patterns. The type of the patterns must equal the type of the head
699702expression.
700703
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-
705704A ` match ` behaves differently depending on whether or not the head expression
706705is an [ lvalue or an rvalue] ( expressions.html#lvalues-rvalues-and-temporaries ) .
707706If the head expression is an rvalue, it is first evaluated into a temporary
@@ -737,19 +736,32 @@ matched value (depending on the matched value's type). This can be changed to
737736bind to a reference by using the ` ref ` keyword, or to a mutable reference using
738737` ref mut ` .
739738
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.
742746
743747``` 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 => " Quit" ,
757+ Message :: WritingString (write ) => & write ,
758+ Message :: Move { x , y : 3 } => " move 3" ,
759+ Message :: Move { .. } => " other move" ,
760+ Message :: ChangeColor { 0 : red , 1 : green , 2 : _ } => " color change" ,
761+ };
750762```
751763
752- Patterns can also dereference pointers by using the ` & ` , ` &mut ` and ` box `
764+ Patterns can also dereference pointers by using the ` & ` and ` &mut `
753765symbols, as appropriate. For example, these two matches on ` x: &i32 ` are
754766equivalent:
755767
@@ -761,6 +773,18 @@ let z = match x { &0 => "zero", _ => "some" };
761773assert_eq! (y , z );
762774```
763775
776+ Subpatterns can also be bound to variables by the use of the syntax `variable @
777+ subpattern`. For example:
778+
779+ ``` rust
780+ let x = 1 ;
781+
782+ match x {
783+ e @ 1 ... 5 => println! (" got a range element {}" , e ),
784+ _ => println! (" anything" ),
785+ }
786+ ```
787+
764788Multiple match patterns may be joined with the ` | ` operator. A range of values
765789may be specified with ` ... ` . For example:
766790
0 commit comments