Skip to content

Fix short hand struct doc #39459

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

Merged
merged 5 commits into from
Feb 8, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add doc field init shorthand
  • Loading branch information
Giang Nguyen authored and phungleson committed Feb 2, 2017
commit a7b65f15781e479d6d0cd98f0865cc3ec0dc03ac
22 changes: 22 additions & 0 deletions src/doc/book/structs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down