Skip to content

Conversation

ghost
Copy link

@ghost ghost commented Jan 16, 2017

Added "field init shorthand" follow by RFC 1682

@ghost ghost mentioned this pull request Jan 16, 2017
5 tasks
Copy link
Contributor

@steveklabnik steveklabnik left a 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:
Copy link
Contributor

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)]
Copy link
Contributor

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,
Copy link
Contributor

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";
Copy link
Contributor

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?

@carols10cents carols10cents added the blocked upstream Blocked on a change that needs to happen in a different repo first label Jan 18, 2017
@carols10cents carols10cents added this to the ch5 milestone Jan 18, 2017
@steveklabnik
Copy link
Contributor

This is now stable in nightly! @HnGiang any interest in fixing up the review issues here?

steveklabnik added a commit that referenced this pull request Mar 23, 2017
From RFC 1682.

This is an updated version of #405.

cc rust-lang/rust#39096
@steveklabnik
Copy link
Contributor

I've updated this and committed it as e90abc5, thank you! ❤️

gypsydave5 pushed a commit to gypsydave5/book that referenced this pull request Nov 17, 2017
From RFC 1682.

This is an updated version of rust-lang#405.

cc rust-lang/rust#39096
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blocked upstream Blocked on a change that needs to happen in a different repo first

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants