From f22dace40976d96a8df44dffa68ad9b0a3d49409 Mon Sep 17 00:00:00 2001 From: steveklabnik Date: Fri, 8 Jun 2018 10:05:42 -0400 Subject: [PATCH] Move the 'new features' appendex into the text Fixes #1294 --- 2018-edition/src/SUMMARY.md | 3 +- .../src/appendix-06-newest-features.md | 128 ------------------ ...ly-rust.md => appendix-06-nightly-rust.md} | 2 +- 2018-edition/src/ch03-05-control-flow.md | 24 ++++ 2018-edition/src/ch04-03-slices.md | 13 +- .../src/ch07-03-importing-names-with-use.md | 33 +++++ 6 files changed, 71 insertions(+), 132 deletions(-) delete mode 100644 2018-edition/src/appendix-06-newest-features.md rename 2018-edition/src/{appendix-07-nightly-rust.md => appendix-06-nightly-rust.md} (99%) diff --git a/2018-edition/src/SUMMARY.md b/2018-edition/src/SUMMARY.md index 1ca086328c..13e3ae6791 100644 --- a/2018-edition/src/SUMMARY.md +++ b/2018-edition/src/SUMMARY.md @@ -128,5 +128,4 @@ - [C - Derivable Traits](appendix-03-derivable-traits.md) - [D - Macros](appendix-04-macros.md) - [E - Translations](appendix-05-translation.md) - - [F - Newest Features](appendix-06-newest-features.md) - - [G - How Rust is Made and “Nightly Rust”](appendix-07-nightly-rust.md) + - [F - How Rust is Made and “Nightly Rust”](appendix-06-nightly-rust.md) diff --git a/2018-edition/src/appendix-06-newest-features.md b/2018-edition/src/appendix-06-newest-features.md deleted file mode 100644 index af226c896f..0000000000 --- a/2018-edition/src/appendix-06-newest-features.md +++ /dev/null @@ -1,128 +0,0 @@ -# Appendix F - Newest Features - -This appendix documents features that have been added to stable Rust since the -main part of the book was completed. - - -## Field init shorthand - -We can initialize 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: - -```rust -#[derive(Debug)] -struct Person { - name: String, - age: u8, -} - -fn main() { - let name = String::from("Peter"); - let age = 27; - - // Using full syntax: - let peter = Person { name: name, age: age }; - - let name = String::from("Portia"); - let age = 27; - - // Using field init shorthand: - let portia = Person { name, age }; - - println!("{:?}", portia); -} -``` - - -## Returning from loops - -One of the uses of a `loop` is to retry an operation you know can fail, such as -checking if a thread completed its job. However, you might need to pass the -result of that operation to the rest of your code. If you add it to the `break` -expression you use to stop the loop, it will be returned by the broken loop: - -```rust -fn main() { - let mut counter = 0; - - let result = loop { - counter += 1; - - if counter == 10 { - break counter * 2; - } - }; - - assert_eq!(result, 20); -} -``` - -## Nested groups in `use` declarations - -If you have a complex module tree with many different submodules and you need -to import a few items from each one, it might be useful to group all the -imports in the same declaration to keep your code clean and avoid repeating the -base modules’ name. - -The `use` declaration supports nesting to help you in those cases, both with -simple imports and glob ones. For example this snippets imports `bar`, `Foo`, -all the items in `baz` and `Bar`: - -```rust -# #![allow(unused_imports, dead_code)] -# -# mod foo { -# pub mod bar { -# pub type Foo = (); -# } -# pub mod baz { -# pub mod quux { -# pub type Bar = (); -# } -# } -# } -# -use foo::{ - bar::{self, Foo}, - baz::{*, quux::Bar}, -}; -# -# fn main() {} -``` - -## Inclusive ranges - -Previously, when a range (`..` or `...`) was used as an expression, it had to be -`..`, which is exclusive of the upper bound, while patterns had to use `...`, -which is inclusive of the upper bound. Now, `..=` is accepted as syntax for -inclusive ranges in both expression and range context: - -```rust -fn main() { - for i in 0 ..= 10 { - match i { - 0 ..= 5 => println!("{}: low", i), - 6 ..= 10 => println!("{}: high", i), - _ => println!("{}: out of range", i), - } - } -} -``` - -The `...` syntax is still accepted in matches, but it is not accepted in -expressions. `..=` should be preferred. - -## 128-bit integers - -Rust 1.26.0 added 128-bit integer primitives: - -- `u128`: A 128-bit unsigned integer with range [0, 2^128 - 1] -- `i128`: A 128-bit signed integer with range [-(2^127), 2^127 - 1] - -These primitives are implemented efficiently via LLVM support. They are -available even on platforms that don’t natively support 128-bit integers and -can be used like the other integer types. - -These primitives can be very useful for algorithms that need to use very large -integers efficiently, such as certain cryptographic algorithms. diff --git a/2018-edition/src/appendix-07-nightly-rust.md b/2018-edition/src/appendix-06-nightly-rust.md similarity index 99% rename from 2018-edition/src/appendix-07-nightly-rust.md rename to 2018-edition/src/appendix-06-nightly-rust.md index 7d091e705e..d276d9ec35 100644 --- a/2018-edition/src/appendix-07-nightly-rust.md +++ b/2018-edition/src/appendix-06-nightly-rust.md @@ -1,4 +1,4 @@ -# Appendix G - How Rust is Made and “Nightly Rust” +# Appendix F - How Rust is Made and “Nightly Rust” This appendix is about how Rust is made and how that affects you as a Rust developer. diff --git a/2018-edition/src/ch03-05-control-flow.md b/2018-edition/src/ch03-05-control-flow.md index 6ea0e32ad7..b6bde5e907 100644 --- a/2018-edition/src/ch03-05-control-flow.md +++ b/2018-edition/src/ch03-05-control-flow.md @@ -309,6 +309,30 @@ stop executing the loop. Recall that we did this in the guessing game in the “Quitting After a Correct Guess” section of Chapter 2 to exit the program when the user won the game by guessing the correct number. + +#### Returning from loops + +One of the uses of a `loop` is to retry an operation you know can fail, such as +checking if a thread completed its job. However, you might need to pass the +result of that operation to the rest of your code. If you add it to the `break` +expression you use to stop the loop, it will be returned by the broken loop: + +```rust +fn main() { + let mut counter = 0; + + let result = loop { + counter += 1; + + if counter == 10 { + break counter * 2; + } + }; + + assert_eq!(result, 20); +} +``` + #### Conditional Loops with `while` It’s often useful for a program to evaluate a condition within a loop. While diff --git a/2018-edition/src/ch04-03-slices.md b/2018-edition/src/ch04-03-slices.md index 8cac17aba9..1abb53654e 100644 --- a/2018-edition/src/ch04-03-slices.md +++ b/2018-edition/src/ch04-03-slices.md @@ -150,7 +150,18 @@ let world = &s[6..11]; This is similar to taking a reference to the whole `String` but with the extra `[0..5]` bit. Rather than a reference to the entire `String`, it’s a reference to a portion of the `String`. The `start..end` syntax is a range that begins at -`start` and continues up to, but not including, `end`. +`start` and continues up to, but not including, `end`. If we wanted to include +`end`, we can use `..=` instead of `..`: + +```rust +let s = String::from("hello world"); + +let hello = &s[0..=4]; +let world = &s[6..=10]; +``` + +The `=` means that we're including the last number, if that helps you remember +the difference between `..` and `..=`. We can create slices using a range within brackets by specifying `[starting_index..ending_index]`, where `starting_index` is the first position diff --git a/2018-edition/src/ch07-03-importing-names-with-use.md b/2018-edition/src/ch07-03-importing-names-with-use.md index 4f3d258f07..1bc7419506 100644 --- a/2018-edition/src/ch07-03-importing-names-with-use.md +++ b/2018-edition/src/ch07-03-importing-names-with-use.md @@ -104,6 +104,39 @@ fn main() { We’re still specifying the `TrafficLight` namespace for the `Green` variant because we didn’t include `Green` in the `use` statement. +#### Nested groups in `use` declarations + +If you have a complex module tree with many different submodules and you need +to import a few items from each one, it might be useful to group all the +imports in the same declaration to keep your code clean and avoid repeating the +base modules’ name. + +The `use` declaration supports nesting to help you in those cases, both with +simple imports and glob ones. For example this snippets imports `bar`, `Foo`, +all the items in `baz` and `Bar`: + +```rust +# #![allow(unused_imports, dead_code)] +# +# mod foo { +# pub mod bar { +# pub type Foo = (); +# } +# pub mod baz { +# pub mod quux { +# pub type Bar = (); +# } +# } +# } +# +use foo::{ + bar::{self, Foo}, + baz::{*, quux::Bar}, +}; +# +# fn main() {} +``` + ### Bringing All Names into Scope with a Glob To bring all the items in a namespace into scope at once, we can use the `*`