From 422895ac8b7486dec2f659ab97c1036f89f6e173 Mon Sep 17 00:00:00 2001 From: Giang Nguyen Date: Mon, 16 Jan 2017 15:06:30 +0700 Subject: [PATCH 1/3] Update grammar struct field init shorthand --- src/doc/grammar.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/doc/grammar.md b/src/doc/grammar.md index 690d44cc2cb7b..3dc09d21e7eea 100644 --- a/src/doc/grammar.md +++ b/src/doc/grammar.md @@ -510,8 +510,9 @@ unit_expr : "()" ; ### Structure expressions ```antlr -struct_expr : expr_path '{' ident ':' expr - [ ',' ident ':' expr ] * +field_init : ident | ident ':' expr; +struct_expr : expr_path '{' field_init + [ ',' field_init ] * [ ".." expr ] '}' | expr_path '(' expr [ ',' expr ] * ')' | From 00650de1a8c323826eae558c304838b2795a3648 Mon Sep 17 00:00:00 2001 From: Giang Nguyen Date: Mon, 16 Jan 2017 16:24:30 +0700 Subject: [PATCH 2/3] Add explain struct field init shorthand --- src/doc/reference.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/doc/reference.md b/src/doc/reference.md index 4112b328f612e..9e821e53a7115 100644 --- a/src/doc/reference.md +++ b/src/doc/reference.md @@ -2757,6 +2757,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 From 66bc3ca0adc13cc5b33270effbc9b798d5e48114 Mon Sep 17 00:00:00 2001 From: Giang Nguyen Date: Mon, 16 Jan 2017 16:58:07 +0700 Subject: [PATCH 3/3] Add doc field init shorthand --- src/doc/book/structs.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/doc/book/structs.md b/src/doc/book/structs.md index cfd00cf997e0b..5a13746d0a87f 100644 --- a/src/doc/book/structs.md +++ b/src/doc/book/structs.md @@ -117,6 +117,28 @@ fn main() { } ``` +We can initializing a data structure (struct, enum, union) with named fields, by writing `fieldname` as a shorthand for `fieldname: fieldname`. This allows a compact syntax for initialization, with less duplication: + +``` +#![feature(field_init_shorthand)] + +#[derive(Debug)] +struct Person<'a> { + name: &'a str, + age: u8 +} + +fn main() { + // Create struct with field init shorthand + let name = "Peter"; + let age = 27; + let peter = Person { name, age }; + + // Print debug struct + println!("{:?}", peter); +} +``` + # Update syntax A `struct` can include `..` to indicate that you want to use a copy of some