diff --git a/listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs b/listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs index b822b89ff3..d44e290d70 100644 --- a/listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs +++ b/listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs @@ -25,7 +25,7 @@ fn main() { // ANCHOR_END: expect // ANCHOR: print_guess - println!("You guessed: {}", guess); + println!("You guessed: {guess}"); // ANCHOR_END: print_guess } // ANCHOR: all diff --git a/listings/ch02-guessing-game-tutorial/listing-02-04/output.txt b/listings/ch02-guessing-game-tutorial/listing-02-04/output.txt index e20f503ce5..ca5e05882f 100644 --- a/listings/ch02-guessing-game-tutorial/listing-02-04/output.txt +++ b/listings/ch02-guessing-game-tutorial/listing-02-04/output.txt @@ -8,20 +8,17 @@ $ cargo build Compiling rand v0.8.5 Compiling guessing_game v0.1.0 (file:///projects/guessing_game) error[E0308]: mismatched types - --> src/main.rs:22:21 - | -22 | match guess.cmp(&secret_number) { - | --- ^^^^^^^^^^^^^^ expected `&String`, found `&{integer}` - | | - | arguments to this method are incorrect - | - = note: expected reference `&String` - found reference `&{integer}` + --> src/main.rs:23:21 + | +23 | match guess.cmp(&secret_number) { + | --- ^^^^^^^^^^^^^^ expected `&String`, found `&{integer}` + | | + | arguments to this method are incorrect + | + = note: expected reference `&String` + found reference `&{integer}` note: method defined here - --> file:///home/.rustup/toolchains/1.85/lib/rustlib/src/rust/library/core/src/cmp.rs:964:8 - | -964 | fn cmp(&self, other: &Self) -> Ordering; - | ^^^ + --> /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/core/src/cmp.rs:964:8 For more information about this error, try `rustc --explain E0308`. error: could not compile `guessing_game` (bin "guessing_game") due to 1 previous error diff --git a/listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/output.txt b/listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/output.txt index 6349339a66..d3ca35ca75 100644 --- a/listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/output.txt +++ b/listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/output.txt @@ -1,5 +1,5 @@ $ cargo run Compiling guessing_game v0.1.0 (file:///projects/guessing_game) Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.08s - Running `file:///projects/guessing_game/target/debug/guessing_game` + Running `target/debug/guessing_game` Hello, world! diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/output.txt b/listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/output.txt index 10c3d2eb86..e8b76379db 100644 --- a/listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/output.txt +++ b/listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/output.txt @@ -1,25 +1,22 @@ $ cargo run Compiling enums v0.1.0 (file:///projects/enums) error[E0004]: non-exhaustive patterns: `None` not covered - --> src/main.rs:3:15 - | -3 | match x { - | ^ pattern `None` not covered - | + --> src/main.rs:3:15 + | +3 | match x { + | ^ pattern `None` not covered + | note: `Option` defined here - --> file:///home/.rustup/toolchains/1.85/lib/rustlib/src/rust/library/core/src/option.rs:572:1 - | -572 | pub enum Option { - | ^^^^^^^^^^^^^^^^^^ -... -576 | None, - | ---- not covered - = note: the matched value is of type `Option` + --> /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/core/src/option.rs:572:1 + ::: /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/core/src/option.rs:576:5 + | + = note: not covered + = note: the matched value is of type `Option` help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown - | -4 ~ Some(i) => Some(i + 1), -5 ~ None => todo!(), - | + | +4 ~ Some(i) => Some(i + 1), +5 ~ None => todo!(), + | For more information about this error, try `rustc --explain E0004`. error: could not compile `enums` (bin "enums") due to 1 previous error diff --git a/listings/ch11-writing-automated-tests/listing-11-01/output.txt b/listings/ch11-writing-automated-tests/listing-11-01/output.txt index 1edbc90ab5..76e144c47e 100644 --- a/listings/ch11-writing-automated-tests/listing-11-01/output.txt +++ b/listings/ch11-writing-automated-tests/listing-11-01/output.txt @@ -1,7 +1,7 @@ $ cargo test Compiling adder v0.1.0 (file:///projects/adder) Finished `test` profile [unoptimized + debuginfo] target(s) in 0.57s - Running unittests src/lib.rs (file:///projects/adder/target/debug/deps/adder-40313d497ef8f64e) + Running unittests src/lib.rs (target/debug/deps/adder-01ad14159ff659ab) running 1 test test tests::it_works ... ok diff --git a/listings/ch15-smart-pointers/listing-15-21/output.txt b/listings/ch15-smart-pointers/listing-15-21/output.txt index 7528ad5ac2..8501007f05 100644 --- a/listings/ch15-smart-pointers/listing-15-21/output.txt +++ b/listings/ch15-smart-pointers/listing-15-21/output.txt @@ -17,4 +17,3 @@ help: consider changing this to be a mutable reference in the `impl` method and For more information about this error, try `rustc --explain E0596`. error: could not compile `limit-tracker` (lib test) due to 1 previous error -warning: build failed, waiting for other jobs to finish... diff --git a/listings/ch15-smart-pointers/output-only-01-comparing-to-reference/output.txt b/listings/ch15-smart-pointers/output-only-01-comparing-to-reference/output.txt index 63eb7959e9..d1eccd2b22 100644 --- a/listings/ch15-smart-pointers/output-only-01-comparing-to-reference/output.txt +++ b/listings/ch15-smart-pointers/output-only-01-comparing-to-reference/output.txt @@ -8,11 +8,6 @@ error[E0277]: can't compare `{integer}` with `&{integer}` | = help: the trait `PartialEq<&{integer}>` is not implemented for `{integer}` = note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info) -help: consider dereferencing here - --> file:///home/.rustup/toolchains/1.85/lib/rustlib/src/rust/library/core/src/macros/mod.rs:46:35 - | -46| if !(*left_val == **right_val) { - | + For more information about this error, try `rustc --explain E0277`. error: could not compile `deref-example` (bin "deref-example") due to 1 previous error diff --git a/listings/ch16-fearless-concurrency/listing-16-14/output.txt b/listings/ch16-fearless-concurrency/listing-16-14/output.txt index 486a064174..131fbed5b7 100644 --- a/listings/ch16-fearless-concurrency/listing-16-14/output.txt +++ b/listings/ch16-fearless-concurrency/listing-16-14/output.txt @@ -1,34 +1,28 @@ $ cargo run Compiling shared-state v0.1.0 (file:///projects/shared-state) error[E0277]: `Rc>` cannot be sent between threads safely - --> src/main.rs:11:36 - | -11 | let handle = thread::spawn(move || { - | ------------- ^------ - | | | - | ______________________|_____________within this `{closure@src/main.rs:11:36: 11:43}` - | | | - | | required by a bound introduced by this call -12 | | let mut num = counter.lock().unwrap(); -13 | | -14 | | *num += 1; -15 | | }); - | |_________^ `Rc>` cannot be sent between threads safely - | - = help: within `{closure@src/main.rs:11:36: 11:43}`, the trait `Send` is not implemented for `Rc>` + --> src/main.rs:11:36 + | +11 | let handle = thread::spawn(move || { + | ------------- ^------ + | | | + | ______________________|_____________within this `{closure@src/main.rs:11:36: 11:43}` + | | | + | | required by a bound introduced by this call +12 | | let mut num = counter.lock().unwrap(); +13 | | +14 | | *num += 1; +15 | | }); + | |_________^ `Rc>` cannot be sent between threads safely + | + = help: within `{closure@src/main.rs:11:36: 11:43}`, the trait `Send` is not implemented for `Rc>` note: required because it's used within this closure - --> src/main.rs:11:36 - | -11 | let handle = thread::spawn(move || { - | ^^^^^^^ + --> src/main.rs:11:36 + | +11 | let handle = thread::spawn(move || { + | ^^^^^^^ note: required by a bound in `spawn` - --> file:///home/.rustup/toolchains/1.85/lib/rustlib/src/rust/library/std/src/thread/mod.rs:731:8 - | -728 | pub fn spawn(f: F) -> JoinHandle - | ----- required by a bound in this function -... -731 | F: Send + 'static, - | ^^^^ required by this bound in `spawn` + --> /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/std/src/thread/mod.rs:728:1 For more information about this error, try `rustc --explain E0277`. error: could not compile `shared-state` (bin "shared-state") due to 1 previous error diff --git a/listings/ch19-patterns-and-matching/listing-19-10/output.txt b/listings/ch19-patterns-and-matching/listing-19-10/output.txt index 97bc014ec1..7f825dcca1 100644 --- a/listings/ch19-patterns-and-matching/listing-19-10/output.txt +++ b/listings/ch19-patterns-and-matching/listing-19-10/output.txt @@ -1,16 +1,15 @@ $ cargo run Compiling patterns v0.1.0 (file:///projects/patterns) -warning: irrefutable `if let` pattern - --> src/main.rs:2:8 +warning: irrefutable `let...else` pattern + --> src/main.rs:2:5 | -2 | if let x = 5 { - | ^^^^^^^^^ +2 | let x = 5 else { + | ^^^^^^^^^ | - = note: this pattern will always match, so the `if let` is useless - = help: consider replacing the `if let` with a `let` + = note: this pattern will always match, so the `else` clause is useless + = help: consider removing the `else` clause = note: `#[warn(irrefutable_let_patterns)]` on by default warning: `patterns` (bin "patterns") generated 1 warning Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.39s Running `target/debug/patterns` -5 diff --git a/listings/ch20-advanced-features/listing-20-33/output.txt b/listings/ch20-advanced-features/listing-20-33/output.txt index e8259121eb..2825c07c7b 100644 --- a/listings/ch20-advanced-features/listing-20-33/output.txt +++ b/listings/ch20-advanced-features/listing-20-33/output.txt @@ -1,20 +1,20 @@ $ cargo build Compiling functions-example v0.1.0 (file:///projects/functions-example) - error[E0308]: mismatched types - --> src/main.rs:4:9 - | - 4 | returns_initialized_closure(123) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected opaque type, found a different opaque type - ... - 12 | fn returns_closure() -> impl Fn(i32) -> i32 { - | ------------------- the expected opaque type - ... - 16 | fn returns_initialized_closure(init: i32) -> impl Fn(i32) -> i32 { - | ------------------- the found opaque type - | - = note: expected opaque type `impl Fn(i32) -> i32` (opaque type at ) - found opaque type `impl Fn(i32) -> i32` (opaque type at ) - = note: distinct uses of `impl Trait` result in different opaque types +error[E0308]: mismatched types + --> src/main.rs:2:44 + | +2 | let handlers = vec![returns_closure(), returns_initialized_closure(123)]; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected opaque type, found a different opaque type +... +9 | fn returns_closure() -> impl Fn(i32) -> i32 { + | ------------------- the expected opaque type +... +13 | fn returns_initialized_closure(init: i32) -> impl Fn(i32) -> i32 { + | ------------------- the found opaque type + | + = note: expected opaque type `impl Fn(i32) -> i32` (opaque type at ) + found opaque type `impl Fn(i32) -> i32` (opaque type at ) + = note: distinct uses of `impl Trait` result in different opaque types - For more information about this error, try `rustc --explain E0308`. - error: could not compile `functions-example` (bin "functions-example") due to 1 previous error +For more information about this error, try `rustc --explain E0308`. +error: could not compile `functions-example` (bin "functions-example") due to 1 previous error diff --git a/listings/ch21-web-server/listing-21-22/output.txt b/listings/ch21-web-server/listing-21-22/output.txt index 9362f927e6..0634b6632f 100644 --- a/listings/ch21-web-server/listing-21-22/output.txt +++ b/listings/ch21-web-server/listing-21-22/output.txt @@ -1,18 +1,15 @@ $ cargo check Checking hello v0.1.0 (file:///projects/hello) error[E0507]: cannot move out of `worker.thread` which is behind a mutable reference - --> src/lib.rs:52:13 - | -52 | worker.thread.join().unwrap(); - | ^^^^^^^^^^^^^ ------ `worker.thread` moved due to this method call - | | - | move occurs because `worker.thread` has type `JoinHandle<()>`, which does not implement the `Copy` trait - | + --> src/lib.rs:52:13 + | +52 | worker.thread.join().unwrap(); + | ^^^^^^^^^^^^^ ------ `worker.thread` moved due to this method call + | | + | move occurs because `worker.thread` has type `JoinHandle<()>`, which does not implement the `Copy` trait + | note: `JoinHandle::::join` takes ownership of the receiver `self`, which moves `worker.thread` - --> file:///home/.rustup/toolchains/1.85/lib/rustlib/src/rust/library/std/src/thread/mod.rs:1876:17 - | -1876 | pub fn join(self) -> Result { - | ^^^^ + --> /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/std/src/thread/mod.rs:1876:17 For more information about this error, try `rustc --explain E0507`. error: could not compile `hello` (lib) due to 1 previous error diff --git a/nostarch/chapter01.md b/nostarch/chapter01.md index 534d42da2b..c4b0d17f54 100644 --- a/nostarch/chapter01.md +++ b/nostarch/chapter01.md @@ -32,7 +32,7 @@ warnings. In other words, any newer, stable version of Rust you install using these steps should work as expected with the content of this book. > ### Command Line Notation -> +> > In this chapter and throughout the book, we’ll show some commands used in the > terminal. Lines that you should enter in a terminal all start with `$`. You > don’t need to type the `$` character; it’s the command line prompt shown to @@ -230,11 +230,11 @@ $ ./main Hello, world! ``` -On Windows, enter the command `.\main.exe` instead of `./main`: +On Windows, enter the command `.\main` instead of `./main`: ``` > rustc main.rs -> .\main.exe +> .\main Hello, world! ``` @@ -283,10 +283,11 @@ This line does all the work in this little program: it prints text to the screen. There are three important details to notice here. First, `println!` calls a Rust macro. If it had called a function instead, it -would be entered as `println` (without the `!`). We’ll discuss Rust macros in -more detail in Chapter 20. For now, you just need to know that using a `!` -means that you’re calling a macro instead of a normal function and that macros -don’t always follow the same rules as functions. +would be entered as `println` (without the `!`). Rust macros are a way to write +code that generates code to extend Rust syntax, and we’ll discuss them in more +detail in Chapter 20. For now, you just need to know that using a `!` means +that you’re calling a macro instead of a normal function and that macros don’t +always follow the same rules as functions. Second, you see the `"Hello, world!"` string. We pass this string as an argument to `println!`, and the string is printed to the screen. @@ -336,7 +337,7 @@ Windows, a file containing debugging information with the *.pdb* extension. From here, you run the *main* or *main.exe* file, like this: ``` -$ ./main # or .\main.exe on Windows +$ ./main # or .\main on Windows ``` If your *main.rs* is your “Hello, world!” program, this line prints `Hello, world!` to your terminal. diff --git a/nostarch/chapter02.md b/nostarch/chapter02.md index 5384a0be2f..a22802fe6f 100644 --- a/nostarch/chapter02.md +++ b/nostarch/chapter02.md @@ -75,7 +75,7 @@ using the `cargo run` command: $ cargo run Compiling guessing_game v0.1.0 (file:///projects/guessing_game) Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.08s - Running `file:///projects/guessing_game/target/debug/guessing_game` + Running `target/debug/guessing_game` Hello, world! ``` @@ -108,7 +108,7 @@ fn main() { .read_line(&mut guess) .expect("Failed to read line"); - println!("You guessed: {}", guess); + println!("You guessed: {guess}"); } ``` @@ -214,7 +214,7 @@ input: .read_line(&mut guess) ``` -If we hadn’t imported the `io` library with `use std::io;` at the beginning of +If we hadn’t imported the `io` module with `use std::io;` at the beginning of the program, we could still use the function by writing this function call as `std::io::stdin`. The `stdin` function returns an instance of `std::io::Stdin`, which is a type that represents a @@ -322,7 +322,7 @@ Aside from the closing curly bracket, there’s only one more line to discuss in the code so far: ``` - println!("You guessed: {}", guess); + println!("You guessed: {guess}"); ``` This line prints the string that now contains the user’s input. The `{}` set of @@ -400,7 +400,7 @@ Filename: Cargo.toml ``` [dependencies] -rand = "0.8.4" +rand = "0.8.5" ``` In the *Cargo.toml* file, everything that follows a header is part of that @@ -566,6 +566,7 @@ src/main.rs ``` use std::io; + use rand::Rng; fn main() { @@ -659,10 +660,11 @@ explain. src/main.rs ``` -use rand::Rng; use std::cmp::Ordering; use std::io; +use rand::Rng; + fn main() { // --snip-- @@ -733,20 +735,17 @@ $ cargo build Compiling rand v0.8.5 Compiling guessing_game v0.1.0 (file:///projects/guessing_game) error[E0308]: mismatched types - --> src/main.rs:22:21 - | -22 | match guess.cmp(&secret_number) { - | --- ^^^^^^^^^^^^^^ expected `&String`, found `&{integer}` - | | - | arguments to this method are incorrect - | - = note: expected reference `&String` - found reference `&{integer}` + --> src/main.rs:23:21 + | +23 | match guess.cmp(&secret_number) { + | --- ^^^^^^^^^^^^^^ expected `&String`, found `&{integer}` + | | + | arguments to this method are incorrect + | + = note: expected reference `&String` + found reference `&{integer}` note: method defined here - --> file:///home/.rustup/toolchains/1.85/lib/rustlib/src/rust/library/core/src/cmp.rs:964:8 - | -964 | fn cmp(&self, other: &Self) -> Ordering; - | ^^^ + --> /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/core/src/cmp.rs:964:8 For more information about this error, try `rustc --explain E0308`. error: could not compile `guessing_game` (bin "guessing_game") due to 1 previous error @@ -1067,10 +1066,11 @@ secret number. Listing 2-6 shows the final code. src/main.rs ``` -use rand::Rng; use std::cmp::Ordering; use std::io; +use rand::Rng; + fn main() { println!("Guess the number!"); diff --git a/nostarch/docx/chapter01.docx b/nostarch/docx/chapter01.docx index 2019fca789..302c18227c 100644 Binary files a/nostarch/docx/chapter01.docx and b/nostarch/docx/chapter01.docx differ diff --git a/nostarch/docx/chapter02.docx b/nostarch/docx/chapter02.docx index a6a22d6a9a..63e703c658 100644 Binary files a/nostarch/docx/chapter02.docx and b/nostarch/docx/chapter02.docx differ diff --git a/src/ch01-02-hello-world.md b/src/ch01-02-hello-world.md index 928caf5b16..6c74dc1f75 100644 --- a/src/ch01-02-hello-world.md +++ b/src/ch01-02-hello-world.md @@ -69,11 +69,11 @@ $ ./main Hello, world! ``` -On Windows, enter the command `.\main.exe` instead of `./main`: +On Windows, enter the command `.\main` instead of `./main`: ```powershell > rustc main.rs -> .\main.exe +> .\main Hello, world! ``` @@ -122,10 +122,11 @@ This line does all the work in this little program: it prints text to the screen. There are three important details to notice here. First, `println!` calls a Rust macro. If it had called a function instead, it -would be entered as `println` (without the `!`). We’ll discuss Rust macros in -more detail in Chapter 20. For now, you just need to know that using a `!` -means that you’re calling a macro instead of a normal function and that macros -don’t always follow the same rules as functions. +would be entered as `println` (without the `!`). Rust macros are a way to write +code that generates code to extend Rust syntax, and we’ll discuss them in more +detail in [Chapter 20][ch20-macros]. For now, you just need to +know that using a `!` means that you’re calling a macro instead of a normal +function and that macros don’t always follow the same rules as functions. Second, you see the `"Hello, world!"` string. We pass this string as an argument to `println!`, and the string is printed to the screen. @@ -175,7 +176,7 @@ Windows, a file containing debugging information with the _.pdb_ extension. From here, you run the _main_ or _main.exe_ file, like this: ```console -$ ./main # or .\main.exe on Windows +$ ./main # or .\main on Windows ``` If your _main.rs_ is your “Hello, world!” program, this line prints `Hello, @@ -197,3 +198,4 @@ real-world Rust programs. [troubleshooting]: ch01-01-installation.html#troubleshooting [devtools]: appendix-04-useful-development-tools.html +[ch20-macros]: ch20-05-macros.html