-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Doc field init shorthand #405
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
Conversation
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.
Thanks for this! Some nits. This is going to have to sit until the feature is actually stabilized, but I'm happy to have it until then!
To get a particular value out of a struct, we can use dot notation. If we | ||
wanted just this user’s email address, we can say `user1.email`. | ||
|
||
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.
could you wrap this to 80 columns please?
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)] |
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.
could you remove this, please? we aren't going to put it in the book until it's stable.
#[derive(Debug)] | ||
struct Person<'a> { | ||
name: &'a str, |
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.
Could you make this a String
?
fn main() { | ||
// Create struct with field init shorthand | ||
let name = "Peter"; |
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.
... and then use String::from
here?
This is now stable in nightly! @HnGiang any interest in fixing up the review issues here? |
From RFC 1682. This is an updated version of #405. cc rust-lang/rust#39096
I've updated this and committed it as e90abc5, thank you! ❤️ |
From RFC 1682. This is an updated version of rust-lang#405. cc rust-lang/rust#39096
Added "field init shorthand" follow by RFC 1682