]> git.proxmox.com Git - rustc.git/blame - src/doc/book/second-edition/src/appendix-07-newest-features.md
New upstream version 1.19.0+dfsg1
[rustc.git] / src / doc / book / second-edition / src / appendix-07-newest-features.md
CommitLineData
cc61c64b
XL
1# Appendix G - Newest Features
2
3This appendix documents features that have been added to stable Rust since the
4main part of the book was completed.
5
6
7## Field init shorthand
8
9We can initialize a data structure (struct, enum, union) with named
10fields, by writing `fieldname` as a shorthand for `fieldname: fieldname`.
11This allows a compact syntax for initialization, with less duplication:
12
13```rust
14#[derive(Debug)]
15struct Person {
16 name: String,
17 age: u8,
18}
19
20fn main() {
21 let name = String::from("Peter");
22 let age = 27;
23
24 // Using full syntax:
25 let peter = Person { name: name, age: age };
26
27 let name = String::from("Portia");
28 let age = 27;
29
30 // Using field init shorthand:
31 let portia = Person { name, age };
7cac9316 32
cc61c64b
XL
33 println!("{:?}", portia);
34}
35```
7cac9316
XL
36
37
38## Returning from loops
39
40One of the uses of a `loop` is to retry an operation you know can fail, such as
41checking if a thread completed its job. However, you might need to pass the
42result of that operation to the rest of your code. If you add it to the `break`
43expression you use to stop the loop, it will be returned by the broken loop:
44
45```rust
46fn main() {
47 let mut counter = 0;
48
49 let result = loop {
50 counter += 1;
51
52 if counter == 10 {
53 break counter * 2;
54 }
55 };
56
57 assert_eq!(result, 20);
58}
59```