From 897029d07273315db8bfb734d5e06abcd79f26c3 Mon Sep 17 00:00:00 2001 From: Giang Nguyen Date: Mon, 16 Jan 2017 15:06:30 +0700 Subject: [PATCH 1/5] Update grammar struct field init shorthand From 3388855443ccfdce427dbbbf8c3d7c3c78bd7b81 Mon Sep 17 00:00:00 2001 From: Giang Nguyen Date: Mon, 16 Jan 2017 16:24:30 +0700 Subject: [PATCH 2/5] 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 dfdfe32882028..32e0f9bbed8d9 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 a7b65f15781e479d6d0cd98f0865cc3ec0dc03ac Mon Sep 17 00:00:00 2001 From: Giang Nguyen Date: Mon, 16 Jan 2017 16:58:07 +0700 Subject: [PATCH 3/5] 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 From c4cd4e19d976240443ef31968e7b763cf83d6fdb Mon Sep 17 00:00:00 2001 From: Son Date: Thu, 2 Feb 2017 22:24:50 +1100 Subject: [PATCH 4/5] Wrap 80 columns --- src/doc/book/structs.md | 6 ++++-- src/doc/reference.md | 8 ++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/doc/book/structs.md b/src/doc/book/structs.md index 5a13746d0a87f..811f7b4196768 100644 --- a/src/doc/book/structs.md +++ b/src/doc/book/structs.md @@ -117,7 +117,9 @@ 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: +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)] @@ -133,7 +135,7 @@ fn main() { let name = "Peter"; let age = 27; let peter = Person { name, age }; - + // Print debug struct println!("{:?}", peter); } diff --git a/src/doc/reference.md b/src/doc/reference.md index 32e0f9bbed8d9..6f2ce5fd8d1c5 100644 --- a/src/doc/reference.md +++ b/src/doc/reference.md @@ -2759,9 +2759,13 @@ 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. +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`. +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: From 4ddb56bf4dfdc607ae0bd2207da189c4c4d4c14a Mon Sep 17 00:00:00 2001 From: Son Date: Fri, 3 Feb 2017 23:51:50 +1100 Subject: [PATCH 5/5] Simplify wording & fix test src/doc --- src/doc/book/structs.md | 6 +++--- src/doc/reference.md | 9 +++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/doc/book/structs.md b/src/doc/book/structs.md index 811f7b4196768..dcf74cbb0a7c4 100644 --- a/src/doc/book/structs.md +++ b/src/doc/book/structs.md @@ -117,9 +117,9 @@ 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: +Initialization of a data structure (struct, enum, union) can be simplified if +fields of the data structure are initialized with variables which has same +names as the fields. ``` #![feature(field_init_shorthand)] diff --git a/src/doc/reference.md b/src/doc/reference.md index 6f2ce5fd8d1c5..8139a712bddb2 100644 --- a/src/doc/reference.md +++ b/src/doc/reference.md @@ -2770,8 +2770,13 @@ shorthand for `field: field`. Example: ``` -let a = SomeStruct { field1, field2: expression, field3 }; -let b = SomeStruct { field1: field1, field2: expression, field3: field3 }; +# #![feature(field_init_shorthand)] +# struct Point3d { x: i32, y: i32, z: i32 } +# let x = 0; +# let y_value = 0; +# let z = 0; +Point3d { x: x, y: y_value, z: z }; +Point3d { x, y: y_value, z }; ``` ### Block expressions