Skip to content

Commit dc5fe39

Browse files
authored
Merge pull request #37 from matthewjasper/clarified-adt-kinds
Document rfc 1506 - clarified adt kinds
2 parents aa6515b + 53d9d8b commit dc5fe39

File tree

1 file changed

+48
-22
lines changed

1 file changed

+48
-22
lines changed

src/expressions.md

+48-22
Original file line numberDiff line numberDiff line change
@@ -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 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.
133135

134136
A _tuple struct expression_ consists of the [path](paths.html) of a [struct
135137
item](items.html#structs), followed by a parenthesized list of one or more
@@ -150,6 +152,7 @@ The following are examples of struct expressions:
150152
Point {x: 10.0, y: 20.0};
151153
NothingInMe {};
152154
TuplePoint(10.0, 20.0);
155+
TuplePoint { 0: 10.0, 1: 20.0 }; // Results in the same value as the above line
153156
let u = game::User {name: "Joe", age: 35, score: 100_000};
154157
some_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

181184
Example:
182185

@@ -777,10 +780,6 @@ variable binding specifications, wildcards (`..`), and placeholders (`_`). A
777780
the patterns. The type of the patterns must equal the type of the head
778781
expression.
779782

780-
In a pattern whose head expression has an `enum` type, a placeholder (`_`)
781-
stands for a *single* data field, whereas a wildcard `..` stands for *all* the
782-
fields of a particular variant.
783-
784783
A `match` behaves differently depending on whether or not the head expression
785784
is an [lvalue or an rvalue](expressions.html#lvalues-rvalues-and-temporaries).
786785
If the head expression is an rvalue, it is first evaluated into a temporary
@@ -816,16 +815,31 @@ matched value (depending on the matched value's type). This can be changed to
816815
bind to a reference by using the `ref` keyword, or to a mutable reference using
817816
`ref mut`.
818817

819-
Subpatterns can also be bound to variables by the use of the syntax `variable @
820-
subpattern`. For example:
818+
Patterns can be used to *destructure* structs, enums, and tuples. Destructuring
819+
breaks a value up into its component pieces. The syntax used is the same as
820+
when creating such values. When destructing a data structure with named (but
821+
not numbered) fields, it is allowed to write `fieldname` as a shorthand for
822+
`fieldname: fieldname`. In a pattern whose head expression has a `struct`,
823+
`enum` or `tupl` type, a placeholder (`_`) stands for a *single* data field,
824+
whereas a wildcard `..` stands for *all* the fields of a particular variant.
821825

822826
```rust
823-
let x = 1;
824-
825-
match x {
826-
e @ 1 ... 5 => println!("got a range element {}", e),
827-
_ => println!("anything"),
828-
}
827+
# enum Message {
828+
# Quit,
829+
# WriteString(String),
830+
# Move { x: i32, y: i32 },
831+
# ChangeColor(u8, u8, u8),
832+
# }
833+
# let message = Message::Quit;
834+
match message {
835+
Message::Quit => println!("Quit"),
836+
Message::WriteString(write) => println!("{}", &write),
837+
Message::Move{ x, y: 0 } => println!("move {} horizontally", x),
838+
Message::Move{ .. } => println!("other move"),
839+
Message::ChangeColor { 0: red, 1: green, 2: _ } => {
840+
println!("color change, red: {}, green: {}", red, green);
841+
}
842+
};
829843
```
830844

831845
Patterns can also dereference pointers by using the `&`, `&mut` and `box`
@@ -840,6 +854,18 @@ let z = match x { &0 => "zero", _ => "some" };
840854
assert_eq!(y, z);
841855
```
842856

857+
Subpatterns can also be bound to variables by the use of the syntax `variable @
858+
subpattern`. For example:
859+
860+
```rust
861+
let x = 1;
862+
863+
match x {
864+
e @ 1 ... 5 => println!("got a range element {}", e),
865+
_ => println!("anything"),
866+
}
867+
```
868+
843869
Multiple match patterns may be joined with the `|` operator. A range of values
844870
may be specified with `...`. For example:
845871

0 commit comments

Comments
 (0)