]> git.proxmox.com Git - rustc.git/blame - src/doc/reference/src/expressions/struct-expr.md
New upstream version 1.36.0+dfsg1
[rustc.git] / src / doc / reference / src / expressions / struct-expr.md
CommitLineData
ea8adc8c
XL
1# Struct expressions
2
0bf4aa26
XL
3> **<sup>Syntax</sup>**\
4> _StructExpression_ :\
5> &nbsp;&nbsp; &nbsp;&nbsp; _StructExprStruct_\
6> &nbsp;&nbsp; | _StructExprTuple_\
7> &nbsp;&nbsp; | _StructExprUnit_
8>
9> _StructExprStruct_ :\
13cf67c4 10> &nbsp;&nbsp; [_PathInExpression_] `{` [_InnerAttribute_]<sup>\*</sup> (_StructExprFields_ | _StructBase_)<sup>?</sup> `}`
0bf4aa26
XL
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>
13cf67c4
XL
19> _StructBase_ :\
20> &nbsp;&nbsp; `..` [_Expression_]
0bf4aa26
XL
21>
22> _StructExprTuple_ :\
23> &nbsp;&nbsp; [_PathInExpression_] `(`\
13cf67c4 24> &nbsp;&nbsp; &nbsp;&nbsp; [_InnerAttribute_]<sup>\*</sup>\
0bf4aa26
XL
25> &nbsp;&nbsp; &nbsp;&nbsp; ( [_Expression_] (`,` [_Expression_])<sup>\*</sup> `,`<sup>?</sup> )<sup>?</sup>\
26> &nbsp;&nbsp; `)`
27>
28> _StructExprUnit_ : [_PathInExpression_]
29
30A _struct expression_ creates a struct or union value. It consists of a path to a [struct]
31or [union] item followed by the values for the fields of the item. There are three forms
32of struct expressions: struct, tuple, and unit.
ea8adc8c
XL
33
34The following are examples of struct expressions:
35
36```rust
37# struct Point { x: f64, y: f64 }
38# struct NothingInMe { }
39# struct TuplePoint(f64, f64);
40# mod game { pub struct User<'a> { pub name: &'a str, pub age: u32, pub score: usize } }
41# struct Cookie; fn some_fn<T>(t: T) {}
42Point {x: 10.0, y: 20.0};
43NothingInMe {};
44TuplePoint(10.0, 20.0);
45TuplePoint { 0: 10.0, 1: 20.0 }; // Results in the same value as the above line
46let u = game::User {name: "Joe", age: 35, score: 100_000};
47some_fn::<Cookie>(Cookie);
48```
49
0bf4aa26
XL
50## Field struct expression
51
52A struct expression with fields enclosed in curly braces allows you to specify the value
53for each individual field in any order. The field name is separated from its value with a
54colon.
55
56A value of a [union] type can also be created using this syntax, except that it must
57specify exactly one field.
ea8adc8c 58
532ac7d7
XL
59## Functional update syntax
60
ea8adc8c
XL
61A struct expression can terminate with the syntax `..` followed by an
62expression to denote a functional update. The expression following `..` (the
532ac7d7
XL
63base) must have the same struct type as the new struct type being formed.
64
65The entire expression uses the given values for the fields that were specified
66and moves or copies the remaining fields from the base expression. As with all
67struct expressions, all of the fields of the struct must be [visible], even
68those not explicitly named.
ea8adc8c
XL
69
70```rust
71# struct Point3d { x: i32, y: i32, z: i32 }
532ac7d7
XL
72let mut base = Point3d {x: 1, y: 2, z: 3};
73let y_ref = &mut base.y;
74Point3d {y: 0, z: 10, .. base}; // OK, only base.x is accessed
75drop(y_ref);
ea8adc8c
XL
76```
77
9fa01778
XL
78Struct expressions with curly braces can't be used directly in a [loop] or [if]
79expression's head, or in the [scrutinee] of an [if let] or [match] expression.
80However, struct expressions can be in used in these situations if they are
81within another expression, for example inside [parentheses].
0bf4aa26
XL
82
83The field names can be decimal integer values to specify indices for constructing tuple
84structs. This can be used with base structs to fill out the remaining indices not
85specified:
86
87```rust
88struct Color(u8, u8, u8);
89let c1 = Color(0, 0, 0); // Typical way of creating a tuple struct.
90let c2 = Color{0: 255, 1: 127, 2: 0}; // Specifying fields by index.
91let c3 = Color{1: 0, ..c2}; // Fill out all other fields using a base struct.
92```
93
94### Struct field init shorthand
ea8adc8c
XL
95
96When initializing a data structure (struct, enum, union) with named (but not
97numbered) fields, it is allowed to write `fieldname` as a shorthand for
98`fieldname: fieldname`. This allows a compact syntax with less duplication.
0bf4aa26 99For example:
ea8adc8c
XL
100
101```rust
102# struct Point3d { x: i32, y: i32, z: i32 }
103# let x = 0;
104# let y_value = 0;
105# let z = 0;
106Point3d { x: x, y: y_value, z: z };
107Point3d { x, y: y_value, z };
108```
ff7c6d11 109
0bf4aa26
XL
110## Tuple struct expression
111
112A struct expression with fields enclosed in parentheses constructs a tuple struct. Though
113it is listed here as a specific expression for completeness, it is equivalent to a [call
114expression] to the tuple struct's constructor. For example:
115
116```rust
117struct Position(i32, i32, i32);
118Position(0, 0, 0); // Typical way of creating a tuple struct.
119let c = Position; // `c` is a function that takes 3 arguments.
120let pos = c(8, 6, 7); // Creates a `Position` value.
121```
122
123## Unit struct expression
124
125A unit struct expression is just the path to a unit struct item. This refers to the unit
126struct's implicit constant of its value. The unit struct value can also be constructed
127with a fieldless struct expression. For example:
128
129```rust
130struct Gamma;
131let a = Gamma; // Gamma unit value.
132let b = Gamma{}; // Exact same value as `a`.
133```
134
13cf67c4
XL
135## Struct expression attributes
136
137[Inner attributes] are allowed directly after the opening brace or parenthesis
138of a struct expression in the same expression contexts as [attributes on block
139expressions].
0bf4aa26 140
ff7c6d11 141[IDENTIFIER]: identifiers.html
13cf67c4 142[Inner attributes]: attributes.html
0bf4aa26
XL
143[TUPLE_INDEX]: tokens.html#integer-literals
144[_Expression_]: expressions.html
13cf67c4 145[_InnerAttribute_]: attributes.html
0bf4aa26 146[_PathInExpression_]: paths.html#paths-in-expressions
13cf67c4 147[attributes on block expressions]: expressions/block-expr.html#attributes-on-block-expressions
0bf4aa26 148[call expression]: expressions/call-expr.html
ff7c6d11 149[if let]: expressions/if-expr.html#if-let-expressions
0bf4aa26
XL
150[if]: expressions/if-expr.html#if-expressions
151[loop]: expressions/loop-expr.html
8faf50e0 152[match]: expressions/match-expr.html
0bf4aa26 153[parentheses]: http://localhost:3000/expressions/grouped-expr.html
0bf4aa26
XL
154[struct]: items/structs.html
155[union]: items/unions.html
156[visible]: visibility-and-privacy.html
9fa01778 157[scrutinee]: glossary.html#scrutinee