-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Document field init shorthand #39096
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @steveklabnik (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
@@ -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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"intializing" -> initialize
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sentence doesn't read write to me at all. I would replace all of a data structure (struct, enum, union) with named fields
with named fields
.
I would also add a sentence saying that a variable of the name of the field also has to be in scope.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems wrong to me.
I would write:
Initialization of a data structure can be simplified if fields of the data structure are initialized with variables which has same names as the fields.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @KalitaAlexey 's suggestion.
|
||
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`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"a union" -> "an union"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As an English speaker, "a union" sounds write to me. Silly little quirks in this language...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@GuillaumeGomez,
It is actually "a union".
Proof: http://www.businessdictionary.com/definition/union.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
English is way too complicated...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@GuillaumeGomez,
Please read http://english.stackexchange.com/questions/266309/why-is-union-an-exception-to-the-a-an-rule
It is actually simple rule.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's always a simple rule, but there are a lot of them, which makes the whole complicated.
Thank you so much @HnGiang ! Could you also wrap this to 80 columns, please? |
☔ The latest upstream changes (presumably #39260) made this pull request unmergeable. Please resolve the merge conflicts. |
Ping @HnGiang ! This is now the last thing that needs to be done before this can be made stable; do you know when you'll be able to fix up this PR? |
@steveklabnik: I haven't been available these days due to the big holiday in my country, so I couldn't reach the topic. At the moment, I have some troubles in work and cannot fix these problems, I will ask @phungleson for help. Thanks. |
…c, r=steveklabnik Fix short hand struct doc Don't want to discredit @HnGiang effort on this issue. I just want to lend a hand to fix this issue rust-lang#38830, it is a very nice feature and is seemingly completed. Fixes rust-lang#39096 r? @steveklabnik
…c, r=steveklabnik Fix short hand struct doc Don't want to discredit @HnGiang effort on this issue. I just want to lend a hand to fix this issue rust-lang#38830, it is a very nice feature and is seemingly completed. Fixes rust-lang#39096 r? @steveklabnik
From RFC 1682. This is an updated version of #405. cc rust-lang/rust#39096
From RFC 1682. This is an updated version of rust-lang#405. cc rust-lang/rust#39096
Update grammar (commit: #422895a)
Add section the book (commit: #66bc3ca) and the new book (PR: #405)
Add reference explain (commit: #00650de)
Add examples to Rust By Example (PR: #837)
Uncomplete a changelog entry