Skip to content

Commit

Permalink
Add doc field init shorthand
Browse files Browse the repository at this point in the history
  • Loading branch information
Giang Nguyen authored and phungleson committed Feb 2, 2017
1 parent 3388855 commit a7b65f1
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/doc/book/structs.md
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

0 comments on commit a7b65f1

Please sign in to comment.