mirror of https://github.com/rust-lang/reference
Add explain struct field init shorthand
This commit is contained in:
parent
3d4f876eb5
commit
0f8a53bfe1
13
reference.md
13
reference.md
|
@ -2765,6 +2765,19 @@ let base = Point3d {x: 1, y: 2, z: 3};
|
|||
Point3d {y: 0, z: 10, .. base};
|
||||
```
|
||||
|
||||
#### Struct field init shorthand
|
||||
|
||||
When initializing a data structure (struct, enum, union) with named fields, allow writing `fieldname` as a shorthand for `fieldname: fieldname`. This allows a compact syntax for initialization, with less duplication.
|
||||
|
||||
In the initializer for a `struct` with named fields, a `union` with named fields, or an enum variant with named fields, accept an identifier `field` as a shorthand for `field: field`.
|
||||
|
||||
Example:
|
||||
|
||||
```
|
||||
let a = SomeStruct { field1, field2: expression, field3 };
|
||||
let b = SomeStruct { field1: field1, field2: expression, field3: field3 };
|
||||
```
|
||||
|
||||
### Block expressions
|
||||
|
||||
A _block expression_ is similar to a module in terms of the declarations that
|
||||
|
|
Loading…
Reference in New Issue