Skip to content

Commit 81088ea

Browse files
authored
Merge pull request #429 from ehuss/grammar-struct-expr
Grammar: Add struct expression.
2 parents e09454d + 7cee92b commit 81088ea

File tree

1 file changed

+92
-31
lines changed

1 file changed

+92
-31
lines changed

src/expressions/struct-expr.md

Lines changed: 92 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
# Struct expressions
22

3-
There are several forms of struct expressions. A _struct expression_ consists
4-
of the [path] of a [struct item](items/structs.html), followed by a
5-
brace-enclosed list of zero or more comma-separated name-value pairs, providing
6-
the field values of a new instance of the struct. A field name can be any
7-
[identifier], and is separated from its value expression by a
8-
colon. In the case of a tuple struct the field names are numbers corresponding
9-
to the position of the field. The numbers must be written in decimal,
10-
containing no underscores and with no leading zeros or integer suffix. A value
11-
of a [union](items/unions.html) type can also be created using this syntax,
12-
except that it must specify exactly one field.
13-
14-
Struct expressions can't be used directly in the head of a [loop]
15-
or an [if], [if let] or [match] expression. But struct expressions can still be
16-
in used inside parentheses, for example.
17-
18-
A _tuple struct expression_ consists of the path of a struct item, followed by
19-
a parenthesized list of one or more comma-separated expressions (in other
20-
words, the path of a struct item followed by a tuple expression). The struct
21-
item must be a tuple struct item.
22-
23-
A _unit-like struct expression_ consists only of the path of a struct item.
3+
> **<sup>Syntax</sup>**\
4+
> _StructExpression_ :\
5+
> &nbsp;&nbsp; &nbsp;&nbsp; _StructExprStruct_\
6+
> &nbsp;&nbsp; | _StructExprTuple_\
7+
> &nbsp;&nbsp; | _StructExprUnit_
8+
>
9+
> _StructExprStruct_ :\
10+
> &nbsp;&nbsp; [_PathInExpression_] `{` (_StructExprFields_ | _StructBase_)<sup>?</sup> `}`
11+
>
12+
> _StructExprFields_ :\
13+
> &nbsp;&nbsp; _StructExprField_ (`,` _StructExprField_)<sup>\*</sup> (`,` _StructBase_ | `,`<sup>?</sup>)
14+
>
15+
> _StructExprField_ :\
16+
> &nbsp;&nbsp; &nbsp;&nbsp; [IDENTIFIER]\
17+
> &nbsp;&nbsp; | ([IDENTIFIER] | [TUPLE_INDEX]) `:` [_Expression_]
18+
>
19+
> _StructBase_ : `..` [_Expression_]
20+
>
21+
> _StructExprTuple_ :\
22+
> &nbsp;&nbsp; [_PathInExpression_] `(`\
23+
> &nbsp;&nbsp; &nbsp;&nbsp; ( [_Expression_] (`,` [_Expression_])<sup>\*</sup> `,`<sup>?</sup> )<sup>?</sup>\
24+
> &nbsp;&nbsp; `)`
25+
>
26+
> _StructExprUnit_ : [_PathInExpression_]
27+
28+
A _struct expression_ creates a struct or union value. It consists of a path to a [struct]
29+
or [union] item followed by the values for the fields of the item. There are three forms
30+
of struct expressions: struct, tuple, and unit.
2431

2532
The following are examples of struct expressions:
2633

@@ -38,8 +45,14 @@ let u = game::User {name: "Joe", age: 35, score: 100_000};
3845
some_fn::<Cookie>(Cookie);
3946
```
4047

41-
A struct expression forms a new value of the named struct type. Note that for a
42-
given *unit-like* struct type, this will always be the same value.
48+
## Field struct expression
49+
50+
A struct expression with fields enclosed in curly braces allows you to specify the value
51+
for each individual field in any order. The field name is separated from its value with a
52+
colon.
53+
54+
A value of a [union] type can also be created using this syntax, except that it must
55+
specify exactly one field.
4356

4457
A struct expression can terminate with the syntax `..` followed by an
4558
expression to denote a functional update. The expression following `..` (the
@@ -48,22 +61,36 @@ entire expression denotes the result of constructing a new struct (with the
4861
same type as the base expression) with the given values for the fields that
4962
were explicitly specified and the values in the base expression for all other
5063
fields. Just as with all struct expressions, all of the fields of the struct
51-
must be [visible](visibility-and-privacy.html), even those not explicitly
52-
named.
64+
must be [visible], even those not explicitly named.
5365

5466
```rust
5567
# struct Point3d { x: i32, y: i32, z: i32 }
5668
let base = Point3d {x: 1, y: 2, z: 3};
5769
Point3d {y: 0, z: 10, .. base};
5870
```
5971

60-
## Struct field init shorthand
72+
Struct expressions with curly braces can't be used directly in the head of a [loop] or an
73+
[if], [if let] or [match] expression. However, struct expressions can be in used in these
74+
situations if they are within another expression, for example inside
75+
[parentheses].
76+
77+
The field names can be decimal integer values to specify indices for constructing tuple
78+
structs. This can be used with base structs to fill out the remaining indices not
79+
specified:
80+
81+
```rust
82+
struct Color(u8, u8, u8);
83+
let c1 = Color(0, 0, 0); // Typical way of creating a tuple struct.
84+
let c2 = Color{0: 255, 1: 127, 2: 0}; // Specifying fields by index.
85+
let c3 = Color{1: 0, ..c2}; // Fill out all other fields using a base struct.
86+
```
87+
88+
### Struct field init shorthand
6189

6290
When initializing a data structure (struct, enum, union) with named (but not
6391
numbered) fields, it is allowed to write `fieldname` as a shorthand for
6492
`fieldname: fieldname`. This allows a compact syntax with less duplication.
65-
66-
Example:
93+
For example:
6794

6895
```rust
6996
# struct Point3d { x: i32, y: i32, z: i32 }
@@ -74,9 +101,43 @@ Point3d { x: x, y: y_value, z: z };
74101
Point3d { x, y: y_value, z };
75102
```
76103

104+
## Tuple struct expression
105+
106+
A struct expression with fields enclosed in parentheses constructs a tuple struct. Though
107+
it is listed here as a specific expression for completeness, it is equivalent to a [call
108+
expression] to the tuple struct's constructor. For example:
109+
110+
```rust
111+
struct Position(i32, i32, i32);
112+
Position(0, 0, 0); // Typical way of creating a tuple struct.
113+
let c = Position; // `c` is a function that takes 3 arguments.
114+
let pos = c(8, 6, 7); // Creates a `Position` value.
115+
```
116+
117+
## Unit struct expression
118+
119+
A unit struct expression is just the path to a unit struct item. This refers to the unit
120+
struct's implicit constant of its value. The unit struct value can also be constructed
121+
with a fieldless struct expression. For example:
122+
123+
```rust
124+
struct Gamma;
125+
let a = Gamma; // Gamma unit value.
126+
let b = Gamma{}; // Exact same value as `a`.
127+
```
128+
129+
77130
[IDENTIFIER]: identifiers.html
78-
[path]: paths.html
79-
[loop]: expressions/loop-expr.html
80-
[if]: expressions/if-expr.html#if-expressions
131+
[TUPLE_INDEX]: tokens.html#integer-literals
132+
[_Expression_]: expressions.html
133+
[_PathInExpression_]: paths.html#paths-in-expressions
134+
[call expression]: expressions/call-expr.html
81135
[if let]: expressions/if-expr.html#if-let-expressions
136+
[if]: expressions/if-expr.html#if-expressions
137+
[loop]: expressions/loop-expr.html
82138
[match]: expressions/match-expr.html
139+
[parentheses]: http://localhost:3000/expressions/grouped-expr.html
140+
[path]: paths.html
141+
[struct]: items/structs.html
142+
[union]: items/unions.html
143+
[visible]: visibility-and-privacy.html

0 commit comments

Comments
 (0)