diff --git a/nostarch/chapter08-error-message-updates.md b/nostarch/chapter08-error-message-updates.md deleted file mode 100644 index 8a7e3b9943..0000000000 --- a/nostarch/chapter08-error-message-updates.md +++ /dev/null @@ -1,15 +0,0 @@ -Please replace the error message on page 133 with this one: - -``` -error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable - --> src/main.rs:6:5 - | -4 | let first = &v[0]; - | - immutable borrow occurs here -5 | -6 | v.push(6); - | ^^^^^^^^^ mutable borrow occurs here -7 | -8 | println!("The first element is: {}", first); - | ----- immutable borrow later used here -``` diff --git a/nostarch/chapter08-listing-8-5.md b/nostarch/chapter08-listing-8-5.md deleted file mode 100644 index 7af3c6acf4..0000000000 --- a/nostarch/chapter08-listing-8-5.md +++ /dev/null @@ -1,13 +0,0 @@ -Please replace the code in Listing 8-5 with this code: - -``` -let v = vec![1, 2, 3, 4, 5]; - -let third: &i32 = &v[2]; -println!("The third element is {}", third); - -match v.get(2) { - Some(third) => println!("The third element is {}", third), - None => println!("There is no third element."), -} -``` \ No newline at end of file diff --git a/nostarch/chapter09-error-message-updates.md b/nostarch/chapter09-error-message-updates.md deleted file mode 100644 index bce8976bd5..0000000000 --- a/nostarch/chapter09-error-message-updates.md +++ /dev/null @@ -1,47 +0,0 @@ -Please use the following message instead of the error message on page 161. - -``` -error[E0277]: the `?` operator can only be used in a function that returns -`Result` or `Option` (or another type that implements `std::ops::Try`) - --> src/main.rs:4:13 - | -4 | let f = File::open("hello.txt")?; - | ^^^^^^^^^^^^^^^^^^^^^^^^ cannot use the `?` operator in a - function that returns `()` - | - = help: the trait `std::ops::Try` is not implemented for `()` - = note: required by `std::ops::Try::from_error` -``` - ---- - -Then, please replace the highlighted paragraph on page 161 with this text: - -This error points out that we’re only allowed to use the `?` operator in a -function that returns `Result`. When you’re writing code in a function -that doesn’t return `Result`, and you want to use `?` when you call other -functions that return `Result`, you have two choices to fix this problem. -One technique is to change the return type of your function to be `Result` if you have no restrictions preventing that. The other technique is to use -a `match` or one of the `Result` methods to handle the `Result` in -whatever way is appropriate. - -The `main` function is special, and there are restrictions on what its return -type must be. One valid return type for main is `()`, and conveniently, another -valid return type is `Result`, as shown here: - -``` -use std::error::Error; -use std::fs::File; - -fn main() -> Result<(), Box> { - let f = File::open("hello.txt")?; - - Ok(()) -} -``` - -The `Box` type is called a *trait object*, which we’ll talk -about in the section “Using Trait Objects that Allow for Values of Different -Types” in Chapter 17. For now, you can read `Box` to mean “any -kind of error.” Using `?` in a `main` function with this return type is allowed. diff --git a/nostarch/chapter09-listing-9-2-update.md b/nostarch/chapter09-listing-9-2-update.md deleted file mode 100644 index bef9dcde70..0000000000 --- a/nostarch/chapter09-listing-9-2-update.md +++ /dev/null @@ -1,50 +0,0 @@ -Please replace the text of Listing 9-2 on page 152-153 with this text: - -``` -$ RUST_BACKTRACE=1 cargo run - Finished dev [unoptimized + debuginfo] target(s) in 0.00s - Running `target/debug/panic` -thread 'main' panicked at 'index out of bounds: the len is 3 but the index is 99', libcore/slice/mod.rs:2448:10 -stack backtrace: - 0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace - at libstd/sys/unix/backtrace/tracing/gcc_s.rs:49 - 1: std::sys_common::backtrace::print - at libstd/sys_common/backtrace.rs:71 - at libstd/sys_common/backtrace.rs:59 - 2: std::panicking::default_hook::{{closure}} - at libstd/panicking.rs:211 - 3: std::panicking::default_hook - at libstd/panicking.rs:227 - 4: as core::panic::BoxMeUp>::get - at libstd/panicking.rs:476 - 5: std::panicking::continue_panic_fmt - at libstd/panicking.rs:390 - 6: std::panicking::try::do_call - at libstd/panicking.rs:325 - 7: core::ptr::drop_in_place - at libcore/panicking.rs:77 - 8: core::ptr::drop_in_place - at libcore/panicking.rs:59 - 9: >::index - at libcore/slice/mod.rs:2448 - 10: core::slice:: for [T]>::index - at libcore/slice/mod.rs:2316 - 11: as core::ops::index::Index>::index - at liballoc/vec.rs:1653 - 12: panic::main - at src/main.rs:4 - 13: std::rt::lang_start::{{closure}} - at libstd/rt.rs:74 - 14: std::panicking::try::do_call - at libstd/rt.rs:59 - at libstd/panicking.rs:310 - 15: macho_symbol_search - at libpanic_unwind/lib.rs:102 - 16: std::alloc::default_alloc_error_hook - at libstd/panicking.rs:289 - at libstd/panic.rs:392 - at libstd/rt.rs:58 - 17: std::rt::lang_start - at libstd/rt.rs:74 - 18: panic::main -``` diff --git a/nostarch/chapter09-listing-9-5-update.md b/nostarch/chapter09-listing-9-5-update.md deleted file mode 100644 index c5163cd632..0000000000 --- a/nostarch/chapter09-listing-9-5-update.md +++ /dev/null @@ -1,21 +0,0 @@ -Please replace the text of Listing 9-5 on page 156 with this text: - -``` -use std::fs::File; -use std::io::ErrorKind; - -fn main() { - let f = File::open("hello.txt"); - - let f = match f { - Ok(file) => file, - Err(error) => match error.kind() { - ErrorKind::NotFound => match File::create("hello.txt") { - Ok(fc) => fc, - Err(e) => panic!("Tried to create file but there was a problem: {:?}", e), - }, - other_error => panic!("There was a problem opening the file: {:?}", other_error), - }, - }; -} -``` \ No newline at end of file diff --git a/nostarch/chapter09-match-update.md b/nostarch/chapter09-match-update.md deleted file mode 100644 index 91998c521f..0000000000 --- a/nostarch/chapter09-match-update.md +++ /dev/null @@ -1,39 +0,0 @@ -Please enter this text to replace the highlighted text that starts with -“However,” at the bottom of page 156 and top of page 157. - ---- - -However, because `File::create` could also fail, we need a second arm in the -inner `match` expression. When the file can’t be created, a different error -message is printed. The second arm of the outer `match` stays the same, so the -program panics on any error besides the missing file error. - -That’s a lot of `match`! The `match` expression is very useful but also very -much a primitive. In Chapter 13, you’ll learn about closures; the `Result` type has many methods that accept a closure and are implemented using -`match` expressions. Using those methods will make your code more concise. A -more seasoned Rustacean might write this code instead of Listing 9-5: - -``` -use std::fs::File; -use std::io::ErrorKind; - -fn main() { - let f = File::open("hello.txt").unwrap_or_else(|error| { - if error.kind() == ErrorKind::NotFound { - File::create("hello.txt").unwrap_or_else(|error| { - panic!("Tried to create file but there was a problem: {:?}", -error); - }) - } else { - panic!("There was a problem opening the file: {:?}", error); - } - }); -} -``` - -Although this code has the same behavior as Listing 9-5, it doesn’t contain any -`match` expressions and is cleaner to read. Come back to this example after -you’ve read Chapter 13, and look up the `unwrap_or_else` method in the standard -library documentation. Many more of these methods can clean up huge nested -`match` expressions when you’re dealing with errors. diff --git a/nostarch/chapter09-read-to-string-update.md b/nostarch/chapter09-read-to-string-update.md deleted file mode 100644 index 6441cbe7da..0000000000 --- a/nostarch/chapter09-read-to-string-update.md +++ /dev/null @@ -1,28 +0,0 @@ -Please insert this text after the paragraph ending in “ergonomic way to write -it” on page 160. - ---- - -Speaking of different ways to write this function, Listing 9-9 shows that -there’s a way to make this even shorter. - -Filename: src/main.rs - -``` -use std::io; -use std::fs; - -fn read_username_from_file() -> Result { - fs::read_to_string("hello.txt") -} -``` - -Listing 9-9: Using `fs::read_to_string` instead of opening and then reading the -file - -Reading a file into a string is a fairly common operation, so Rust provides the -convenient `fs::read_to_string` function that opens the file, creates a new -`String`, reads the contents of the file, puts the contents into that `String`, -and returns it. Of course, using `fs::read_to_string` doesn’t give us the -opportunity to explain all the error handling, so we did it the longer way -first. diff --git a/nostarch/chapter10-traits-as-parameters.md b/nostarch/chapter10-traits-as-parameters.md deleted file mode 100644 index daa779322b..0000000000 --- a/nostarch/chapter10-traits-as-parameters.md +++ /dev/null @@ -1,174 +0,0 @@ -Please replace the “Trait Bounds” section on page 182-183 with this text, which -should then be followed by the existing “Fixing the largest Function with Trait -Bounds” section. - -### Traits as Parameters - -Now that you know how to define and implement traits, we can explore how to use -traits to define functions that accept many different types. - -For example, in Listing 10-13, we implemented the `Summary` trait on the -`NewsArticle` and `Tweet` types. We can define a `notify` function that calls -the `summarize` method on its `item` parameter, which is of some type that -implements the `Summary` trait. To do this, we can use the `impl Trait` -syntax, like this: - -``` -pub fn notify(item: impl Summary) { - println!("Breaking news! {}", item.summarize()); -} -``` - -Instead of a concrete type for the `item` parameter, we specify the `impl` -keyword and the trait name. This parameter accepts any type that implements the -specified trait. In the body of `notify`, we can call any methods on `item` -that come from the `Summary` trait, such as `summarize`. We can call `notify` -and pass in any instance of `NewsArticle` or `Tweet`. Code that calls the -function with any other type, such as a `String` or an `i32`, won’t compile -because those types don’t implement `Summary`. - -#### Trait Bound Syntax - -The `impl Trait` syntax works for straightforward cases but is actually -syntax sugar for a longer form, which is called a *trait bound*; it looks like -this: - -``` -pub fn notify(item: T) { - println!("Breaking news! {}", item.summarize()); -} -``` - -This longer form is equivalent to the example in the previous section but is -more verbose. We place trait bounds with the declaration of the generic type -parameter after a colon and inside angle brackets. - -The `impl Trait` syntax is convenient and makes for more concise code in simple -cases. The trait bound syntax can express more complexity in other cases. For -example, we can have two parameters that implement `Summary`. Using the `impl -Trait` syntax looks like this: - -``` -pub fn notify(item1: impl Summary, item2: impl Summary) { -``` - -If we wanted this function to allow `item1` and `item2` to have different -types, using `impl Trait` would be appropriate (as long as both types implement -`Summary`). If we wanted to force both parameters to have the same type, that’s -only possible to express using a trait bound, like this: - -``` -pub fn notify(item1: T, item2: T) { -``` - -The generic type `T` specified as the type of the `item1` and `item2` -parameters constrains the function such that the concrete type of the value -passed as an argument for `item1` and `item2` must be the same. - -#### Specifying Multiple Trait Bounds with the `+` Syntax - -We can also specify more than one trait bound. Say we wanted `notify` to use -display formatting on `item` as well as the `summarize` method: we specify in -the `notify` definition that `item` must implement both `Display` and -`Summary`. We can do so using the `+` syntax: - -``` -pub fn notify(item: impl Summary + Display) { -``` - -The `+` syntax is also valid with trait bounds on generic types: - -``` -pub fn notify(item: T) { -``` - -With the two trait bounds specified, the body of `notify` can call `summarize` -and use `{}` to format `item`. - -#### Clearer Trait Bounds with `where` Clauses - -Using too many trait bounds has its downsides. Each generic has its own trait -bounds, so functions with multiple generic type parameters can contain lots of -trait bound information between the function’s name and its parameter list, -making the function signature hard to read. For this reason, Rust has alternate -syntax for specifying trait bounds inside a `where` clause after the function -signature. So instead of writing this: - -``` -fn some_function(t: T, u: U) -> i32 { -``` - -we can use a `where` clause, like this: - -``` -fn some_function(t: T, u: U) -> i32 - where T: Display + Clone, - U: Clone + Debug -{ -``` - -This function’s signature is less cluttered: the function name, parameter list, -and return type are close together, similar to a function without lots of trait -bounds. - -### Returning Types that Implement Traits - -We can also use the `impl Trait` syntax in the return position to return a -value of some type that implements a trait, as shown here: - -``` -fn returns_summarizable() -> impl Summary { - Tweet { - username: String::from("horse_ebooks"), - content: String::from("of course, as you probably already know, -people"), - reply: false, - retweet: false, - } -} -``` - -By using `impl Summary` for the return type, we specify that the -`returns_summarizable` function returns some type that implements the `Summary` -trait without naming the concrete type. In this case, `returns_summarizable` -returns a `Tweet`, but the code calling this function doesn’t know that. - -The ability to return a type that is only specified by the trait it implements -is especially useful in the context of closures and iterators, which we cover -in Chapter 13. Closures and iterators create types that only the compiler knows -or types that are very long to specify. The `impl Trait` syntax lets you -concisely specify that a function returns some type that implements the -`Iterator` trait without needing to write out a very long type. - -However, you can only use `impl Trait` if you’re returning a single type. For -example, this code that returns either a `NewsArticle` or a `Tweet` with the -return type specified as `impl Summary` wouldn’t work: - -``` -fn returns_summarizable(switch: bool) -> impl Summary { - if switch { - NewsArticle { - headline: String::from("Penguins win the Stanley Cup -Championship!"), - location: String::from("Pittsburgh, PA, USA"), - author: String::from("Iceburgh"), - content: String::from("The Pittsburgh Penguins once again are the -best - hockey team in the NHL."), - } - } else { - Tweet { - username: String::from("horse_ebooks"), - content: String::from("of course, as you probably already know, -people"), - reply: false, - retweet: false, - } - } -} -``` - -Returning either a `NewsArticle` or a `Tweet` isn’t allowed due to restrictions -around how the `impl Trait` syntax is implemented in the compiler. We’ll cover -how to write a function with this behavior in the section “Using Trait Objects -that Allow for Values of Different Types” in Chapter 17 on page XX. diff --git a/nostarch/docx/chapter08-error-message-updates.docx b/nostarch/docx/chapter08-error-message-updates.docx deleted file mode 100755 index 8949ee02d0..0000000000 Binary files a/nostarch/docx/chapter08-error-message-updates.docx and /dev/null differ diff --git a/nostarch/docx/chapter09-error-message-updates.docx b/nostarch/docx/chapter09-error-message-updates.docx deleted file mode 100644 index 633df30c01..0000000000 Binary files a/nostarch/docx/chapter09-error-message-updates.docx and /dev/null differ diff --git a/nostarch/docx/chapter09-match-update.docx b/nostarch/docx/chapter09-match-update.docx deleted file mode 100644 index 856129f6f8..0000000000 Binary files a/nostarch/docx/chapter09-match-update.docx and /dev/null differ diff --git a/nostarch/docx/chapter09-read-to-string-update.docx b/nostarch/docx/chapter09-read-to-string-update.docx deleted file mode 100644 index 267dffbc9c..0000000000 Binary files a/nostarch/docx/chapter09-read-to-string-update.docx and /dev/null differ diff --git a/nostarch/docx/chapter10-traits-as-parameters.docx b/nostarch/docx/chapter10-traits-as-parameters.docx deleted file mode 100644 index dadcc5cac4..0000000000 Binary files a/nostarch/docx/chapter10-traits-as-parameters.docx and /dev/null differ