Skip to content

Commit 00650de

Browse files
author
Giang Nguyen
authored
Add explain struct field init shorthand
1 parent 422895a commit 00650de

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/doc/reference.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2757,6 +2757,19 @@ let base = Point3d {x: 1, y: 2, z: 3};
27572757
Point3d {y: 0, z: 10, .. base};
27582758
```
27592759

2760+
#### Struct field init shorthand
2761+
2762+
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.
2763+
2764+
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`.
2765+
2766+
Example:
2767+
2768+
```
2769+
let a = SomeStruct { field1, field2: expression, field3 };
2770+
let b = SomeStruct { field1: field1, field2: expression, field3: field3 };
2771+
```
2772+
27602773
### Block expressions
27612774

27622775
A _block expression_ is similar to a module in terms of the declarations that

0 commit comments

Comments
 (0)