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 805241a5a..b822b89ff 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 @@ -1,14 +1,31 @@ +// ANCHOR: all +// ANCHOR: io use std::io; +// ANCHOR_END: io +// ANCHOR: main fn main() { + // ANCHOR_END: main + // ANCHOR: print println!("Guess the number!"); println!("Please input your guess."); + // ANCHOR_END: print + // ANCHOR: string let mut guess = String::new(); + // ANCHOR_END: string - io::stdin().read_line(&mut guess) + // ANCHOR: read + io::stdin() + .read_line(&mut guess) + // ANCHOR_END: read + // ANCHOR: expect .expect("Failed to read line"); + // ANCHOR_END: expect + // ANCHOR: print_guess println!("You guessed: {}", guess); + // ANCHOR_END: print_guess } +// ANCHOR: all diff --git a/listings/ch02-guessing-game-tutorial/listing-02-02/src/main.rs b/listings/ch02-guessing-game-tutorial/listing-02-02/src/main.rs index 805241a5a..60fb2a8e5 100644 --- a/listings/ch02-guessing-game-tutorial/listing-02-02/src/main.rs +++ b/listings/ch02-guessing-game-tutorial/listing-02-02/src/main.rs @@ -7,7 +7,8 @@ fn main() { let mut guess = String::new(); - io::stdin().read_line(&mut guess) + io::stdin() + .read_line(&mut guess) .expect("Failed to read line"); println!("You guessed: {}", guess); diff --git a/listings/ch02-guessing-game-tutorial/listing-02-03/src/main.rs b/listings/ch02-guessing-game-tutorial/listing-02-03/src/main.rs index b73f36789..d5e3df7f6 100644 --- a/listings/ch02-guessing-game-tutorial/listing-02-03/src/main.rs +++ b/listings/ch02-guessing-game-tutorial/listing-02-03/src/main.rs @@ -17,7 +17,8 @@ fn main() { let mut guess = String::new(); - io::stdin().read_line(&mut guess) + io::stdin() + .read_line(&mut guess) .expect("Failed to read line"); println!("You guessed: {}", guess); diff --git a/listings/ch02-guessing-game-tutorial/listing-02-04/src/main.rs b/listings/ch02-guessing-game-tutorial/listing-02-04/src/main.rs index bee037d4f..ac432df3d 100644 --- a/listings/ch02-guessing-game-tutorial/listing-02-04/src/main.rs +++ b/listings/ch02-guessing-game-tutorial/listing-02-04/src/main.rs @@ -1,10 +1,9 @@ // ANCHOR: here -use std::io; -use std::cmp::Ordering; use rand::Rng; +use std::cmp::Ordering; +use std::io; fn main() { - // --snip-- // ANCHOR_END: here println!("Guess the number!"); @@ -17,7 +16,8 @@ fn main() { let mut guess = String::new(); - io::stdin().read_line(&mut guess) + io::stdin() + .read_line(&mut guess) .expect("Failed to read line"); // ANCHOR: here diff --git a/listings/ch02-guessing-game-tutorial/listing-02-05/src/main.rs b/listings/ch02-guessing-game-tutorial/listing-02-05/src/main.rs index 84bf59c9e..e4f4f87d9 100644 --- a/listings/ch02-guessing-game-tutorial/listing-02-05/src/main.rs +++ b/listings/ch02-guessing-game-tutorial/listing-02-05/src/main.rs @@ -1,6 +1,6 @@ -use std::io; -use std::cmp::Ordering; use rand::Rng; +use std::cmp::Ordering; +use std::io; fn main() { println!("Guess the number!"); @@ -17,7 +17,8 @@ fn main() { // ANCHOR: here // --snip-- - io::stdin().read_line(&mut guess) + io::stdin() + .read_line(&mut guess) .expect("Failed to read line"); // ANCHOR: ch19 diff --git a/listings/ch02-guessing-game-tutorial/listing-02-06/src/main.rs b/listings/ch02-guessing-game-tutorial/listing-02-06/src/main.rs index 2d4254eac..0d4006e94 100644 --- a/listings/ch02-guessing-game-tutorial/listing-02-06/src/main.rs +++ b/listings/ch02-guessing-game-tutorial/listing-02-06/src/main.rs @@ -1,6 +1,6 @@ -use std::io; -use std::cmp::Ordering; use rand::Rng; +use std::cmp::Ordering; +use std::io; fn main() { println!("Guess the number!"); @@ -12,7 +12,8 @@ fn main() { let mut guess = String::new(); - io::stdin().read_line(&mut guess) + io::stdin() + .read_line(&mut guess) .expect("Failed to read line"); let guess: u32 = match guess.trim().parse() { diff --git a/listings/ch02-guessing-game-tutorial/no-listing-03-convert-string-to-number/src/main.rs b/listings/ch02-guessing-game-tutorial/no-listing-03-convert-string-to-number/src/main.rs index b20603789..44d8db6b8 100644 --- a/listings/ch02-guessing-game-tutorial/no-listing-03-convert-string-to-number/src/main.rs +++ b/listings/ch02-guessing-game-tutorial/no-listing-03-convert-string-to-number/src/main.rs @@ -1,6 +1,6 @@ -use std::io; -use std::cmp::Ordering; use rand::Rng; +use std::cmp::Ordering; +use std::io; fn main() { println!("Guess the number!"); @@ -16,11 +16,11 @@ fn main() { let mut guess = String::new(); - io::stdin().read_line(&mut guess) + io::stdin() + .read_line(&mut guess) .expect("Failed to read line"); - let guess: u32 = guess.trim().parse() - .expect("Please type a number!"); + let guess: u32 = guess.trim().parse().expect("Please type a number!"); println!("You guessed: {}", guess); diff --git a/listings/ch02-guessing-game-tutorial/no-listing-04-looping/src/main.rs b/listings/ch02-guessing-game-tutorial/no-listing-04-looping/src/main.rs index 2efd6243e..5b42f6557 100644 --- a/listings/ch02-guessing-game-tutorial/no-listing-04-looping/src/main.rs +++ b/listings/ch02-guessing-game-tutorial/no-listing-04-looping/src/main.rs @@ -1,6 +1,6 @@ -use std::io; -use std::cmp::Ordering; use rand::Rng; +use std::cmp::Ordering; +use std::io; fn main() { println!("Guess the number!"); @@ -21,11 +21,11 @@ fn main() { let mut guess = String::new(); - io::stdin().read_line(&mut guess) + io::stdin() + .read_line(&mut guess) .expect("Failed to read line"); - let guess: u32 = guess.trim().parse() - .expect("Please type a number!"); + let guess: u32 = guess.trim().parse().expect("Please type a number!"); println!("You guessed: {}", guess); @@ -37,4 +37,4 @@ fn main() { } } } -// ANCHOR_END: here \ No newline at end of file +// ANCHOR_END: here diff --git a/listings/ch02-guessing-game-tutorial/no-listing-05-quitting/src/main.rs b/listings/ch02-guessing-game-tutorial/no-listing-05-quitting/src/main.rs index c6975ea0e..b32fadbb1 100644 --- a/listings/ch02-guessing-game-tutorial/no-listing-05-quitting/src/main.rs +++ b/listings/ch02-guessing-game-tutorial/no-listing-05-quitting/src/main.rs @@ -1,6 +1,6 @@ -use std::io; -use std::cmp::Ordering; use rand::Rng; +use std::cmp::Ordering; +use std::io; fn main() { println!("Guess the number!"); @@ -14,11 +14,11 @@ fn main() { let mut guess = String::new(); - io::stdin().read_line(&mut guess) + io::stdin() + .read_line(&mut guess) .expect("Failed to read line"); - let guess: u32 = guess.trim().parse() - .expect("Please type a number!"); + let guess: u32 = guess.trim().parse().expect("Please type a number!"); println!("You guessed: {}", guess); diff --git a/listings/ch03-common-programming-concepts/listing-03-02/src/main.rs b/listings/ch03-common-programming-concepts/listing-03-02/src/main.rs index 11ebad4e7..0b8ee95fd 100644 --- a/listings/ch03-common-programming-concepts/listing-03-02/src/main.rs +++ b/listings/ch03-common-programming-concepts/listing-03-02/src/main.rs @@ -1,10 +1,6 @@ fn main() { let condition = true; - let number = if condition { - 5 - } else { - 6 - }; + let number = if condition { 5 } else { 6 }; println!("The value of number is: {}", number); } diff --git a/listings/ch03-common-programming-concepts/no-listing-31-arms-must-return-same-type/output.txt b/listings/ch03-common-programming-concepts/no-listing-31-arms-must-return-same-type/output.txt index 591e73ab9..14b963788 100644 --- a/listings/ch03-common-programming-concepts/no-listing-31-arms-must-return-same-type/output.txt +++ b/listings/ch03-common-programming-concepts/no-listing-31-arms-must-return-same-type/output.txt @@ -1,17 +1,12 @@ $ cargo run Compiling branches v0.1.0 (file:///projects/branches) error[E0308]: if and else have incompatible types - --> src/main.rs:7:9 + --> src/main.rs:4:44 | -4 | let number = if condition { - | __________________- -5 | | 5 - | | - expected because of this -6 | | } else { -7 | | "six" - | | ^^^^^ expected integer, found &str -8 | | }; - | |_____- if and else have incompatible types +4 | let number = if condition { 5 } else { "six" }; + | - ^^^^^ expected integer, found &str + | | + | expected because of this | = note: expected type `{integer}` found type `&str` diff --git a/listings/ch03-common-programming-concepts/no-listing-31-arms-must-return-same-type/src/main.rs b/listings/ch03-common-programming-concepts/no-listing-31-arms-must-return-same-type/src/main.rs index ca1ac9705..440b286f5 100644 --- a/listings/ch03-common-programming-concepts/no-listing-31-arms-must-return-same-type/src/main.rs +++ b/listings/ch03-common-programming-concepts/no-listing-31-arms-must-return-same-type/src/main.rs @@ -1,11 +1,7 @@ fn main() { let condition = true; - let number = if condition { - 5 - } else { - "six" - }; + let number = if condition { 5 } else { "six" }; println!("The value of number is: {}", number); } diff --git a/listings/ch04-understanding-ownership/listing-04-07/src/main.rs b/listings/ch04-understanding-ownership/listing-04-07/src/main.rs index a5ed88907..3bb3c8580 100644 --- a/listings/ch04-understanding-ownership/listing-04-07/src/main.rs +++ b/listings/ch04-understanding-ownership/listing-04-07/src/main.rs @@ -18,5 +18,4 @@ fn first_word(s: &String) -> usize { } // ANCHOR_END: here -fn main() { -} +fn main() {} diff --git a/listings/ch04-understanding-ownership/no-listing-01-can-mutate-string/src/main.rs b/listings/ch04-understanding-ownership/no-listing-01-can-mutate-string/src/main.rs index 86e1ffb49..b68f0f1e7 100644 --- a/listings/ch04-understanding-ownership/no-listing-01-can-mutate-string/src/main.rs +++ b/listings/ch04-understanding-ownership/no-listing-01-can-mutate-string/src/main.rs @@ -5,5 +5,5 @@ fn main() { s.push_str(", world!"); // push_str() appends a literal to a String println!("{}", s); // This will print `hello, world!` - // ANCHOR_END: here + // ANCHOR_END: here } diff --git a/listings/ch04-understanding-ownership/no-listing-11-muts-in-separate-scopes/src/main.rs b/listings/ch04-understanding-ownership/no-listing-11-muts-in-separate-scopes/src/main.rs index 36a4cbddc..4b1a5a383 100644 --- a/listings/ch04-understanding-ownership/no-listing-11-muts-in-separate-scopes/src/main.rs +++ b/listings/ch04-understanding-ownership/no-listing-11-muts-in-separate-scopes/src/main.rs @@ -4,7 +4,6 @@ fn main() { { let r1 = &mut s; - } // r1 goes out of scope here, so we can make a new reference with no problems. let r2 = &mut s; diff --git a/listings/ch04-understanding-ownership/no-listing-18-first-word-slice/src/main.rs b/listings/ch04-understanding-ownership/no-listing-18-first-word-slice/src/main.rs index ffe53251e..f44a970da 100644 --- a/listings/ch04-understanding-ownership/no-listing-18-first-word-slice/src/main.rs +++ b/listings/ch04-understanding-ownership/no-listing-18-first-word-slice/src/main.rs @@ -12,5 +12,4 @@ fn first_word(s: &String) -> &str { } // ANCHOR_END: here -fn main() { -} +fn main() {} diff --git a/listings/ch05-using-structs-to-structure-related-data/listing-05-01/src/main.rs b/listings/ch05-using-structs-to-structure-related-data/listing-05-01/src/main.rs index 01bf0aa61..a7cff6ec2 100644 --- a/listings/ch05-using-structs-to-structure-related-data/listing-05-01/src/main.rs +++ b/listings/ch05-using-structs-to-structure-related-data/listing-05-01/src/main.rs @@ -7,5 +7,4 @@ struct User { } // ANCHOR_END: here -fn main() { -} +fn main() {} diff --git a/listings/ch05-using-structs-to-structure-related-data/listing-05-03/src/main.rs b/listings/ch05-using-structs-to-structure-related-data/listing-05-03/src/main.rs index 15919f68b..c599c9d60 100644 --- a/listings/ch05-using-structs-to-structure-related-data/listing-05-03/src/main.rs +++ b/listings/ch05-using-structs-to-structure-related-data/listing-05-03/src/main.rs @@ -16,4 +16,4 @@ fn main() { user1.email = String::from("anotheremail@example.com"); // ANCHOR_END: here -} \ No newline at end of file +} diff --git a/listings/ch05-using-structs-to-structure-related-data/listing-05-04/src/main.rs b/listings/ch05-using-structs-to-structure-related-data/listing-05-04/src/main.rs index e81e37c86..f934d4c00 100644 --- a/listings/ch05-using-structs-to-structure-related-data/listing-05-04/src/main.rs +++ b/listings/ch05-using-structs-to-structure-related-data/listing-05-04/src/main.rs @@ -17,5 +17,8 @@ fn build_user(email: String, username: String) -> User { // ANCHOR_END: here fn main() { - let user1 = build_user(String::from("someone@example.com"), String::from("someusername123")); -} \ No newline at end of file + let user1 = build_user( + String::from("someone@example.com"), + String::from("someusername123"), + ); +} diff --git a/listings/ch05-using-structs-to-structure-related-data/listing-05-05/src/main.rs b/listings/ch05-using-structs-to-structure-related-data/listing-05-05/src/main.rs index 40c9d7c9e..1833aa8e4 100644 --- a/listings/ch05-using-structs-to-structure-related-data/listing-05-05/src/main.rs +++ b/listings/ch05-using-structs-to-structure-related-data/listing-05-05/src/main.rs @@ -17,5 +17,8 @@ fn build_user(email: String, username: String) -> User { // ANCHOR_END: here fn main() { - let user1 = build_user(String::from("someone@example.com"), String::from("someusername123")); -} \ No newline at end of file + let user1 = build_user( + String::from("someone@example.com"), + String::from("someusername123"), + ); +} diff --git a/listings/ch05-using-structs-to-structure-related-data/listing-05-10/src/main.rs b/listings/ch05-using-structs-to-structure-related-data/listing-05-10/src/main.rs index 67cabcc19..62ef9acd8 100644 --- a/listings/ch05-using-structs-to-structure-related-data/listing-05-10/src/main.rs +++ b/listings/ch05-using-structs-to-structure-related-data/listing-05-10/src/main.rs @@ -4,7 +4,10 @@ struct Rectangle { } fn main() { - let rect1 = Rectangle { width: 30, height: 50 }; + let rect1 = Rectangle { + width: 30, + height: 50, + }; println!( "The area of the rectangle is {} square pixels.", diff --git a/listings/ch05-using-structs-to-structure-related-data/listing-05-11/output.txt b/listings/ch05-using-structs-to-structure-related-data/listing-05-11/output.txt index eb3e23f91..a645708d6 100644 --- a/listings/ch05-using-structs-to-structure-related-data/listing-05-11/output.txt +++ b/listings/ch05-using-structs-to-structure-related-data/listing-05-11/output.txt @@ -1,14 +1,14 @@ $ cargo run Compiling structs v0.1.0 (file:///projects/structs) error[E0277]: `Rectangle` doesn't implement `std::fmt::Display` - --> src/main.rs:9:29 - | -9 | println!("rect1 is {}", rect1); - | ^^^^^ `Rectangle` cannot be formatted with the default formatter - | - = help: the trait `std::fmt::Display` is not implemented for `Rectangle` - = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead - = note: required by `std::fmt::Display::fmt` + --> src/main.rs:12:29 + | +12 | println!("rect1 is {}", rect1); + | ^^^^^ `Rectangle` cannot be formatted with the default formatter + | + = help: the trait `std::fmt::Display` is not implemented for `Rectangle` + = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead + = note: required by `std::fmt::Display::fmt` error: aborting due to previous error diff --git a/listings/ch05-using-structs-to-structure-related-data/listing-05-11/src/main.rs b/listings/ch05-using-structs-to-structure-related-data/listing-05-11/src/main.rs index e494ffc95..0ff8dcc8c 100644 --- a/listings/ch05-using-structs-to-structure-related-data/listing-05-11/src/main.rs +++ b/listings/ch05-using-structs-to-structure-related-data/listing-05-11/src/main.rs @@ -4,7 +4,10 @@ struct Rectangle { } fn main() { - let rect1 = Rectangle { width: 30, height: 50 }; + let rect1 = Rectangle { + width: 30, + height: 50, + }; println!("rect1 is {}", rect1); } diff --git a/listings/ch05-using-structs-to-structure-related-data/listing-05-12/src/main.rs b/listings/ch05-using-structs-to-structure-related-data/listing-05-12/src/main.rs index 3f33dd743..2ffc4b8e7 100644 --- a/listings/ch05-using-structs-to-structure-related-data/listing-05-12/src/main.rs +++ b/listings/ch05-using-structs-to-structure-related-data/listing-05-12/src/main.rs @@ -5,7 +5,10 @@ struct Rectangle { } fn main() { - let rect1 = Rectangle { width: 30, height: 50 }; + let rect1 = Rectangle { + width: 30, + height: 50, + }; println!("rect1 is {:?}", rect1); } diff --git a/listings/ch05-using-structs-to-structure-related-data/listing-05-13/src/main.rs b/listings/ch05-using-structs-to-structure-related-data/listing-05-13/src/main.rs index 7b39830cf..e4f45e868 100644 --- a/listings/ch05-using-structs-to-structure-related-data/listing-05-13/src/main.rs +++ b/listings/ch05-using-structs-to-structure-related-data/listing-05-13/src/main.rs @@ -11,7 +11,10 @@ impl Rectangle { } fn main() { - let rect1 = Rectangle { width: 30, height: 50 }; + let rect1 = Rectangle { + width: 30, + height: 50, + }; println!( "The area of the rectangle is {} square pixels.", diff --git a/listings/ch05-using-structs-to-structure-related-data/listing-05-14/src/main.rs b/listings/ch05-using-structs-to-structure-related-data/listing-05-14/src/main.rs index 29283d083..843dab481 100644 --- a/listings/ch05-using-structs-to-structure-related-data/listing-05-14/src/main.rs +++ b/listings/ch05-using-structs-to-structure-related-data/listing-05-14/src/main.rs @@ -1,7 +1,16 @@ fn main() { - let rect1 = Rectangle { width: 30, height: 50 }; - let rect2 = Rectangle { width: 10, height: 40 }; - let rect3 = Rectangle { width: 60, height: 45 }; + let rect1 = Rectangle { + width: 30, + height: 50, + }; + let rect2 = Rectangle { + width: 10, + height: 40, + }; + let rect3 = Rectangle { + width: 60, + height: 45, + }; println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2)); println!("Can rect1 hold rect3? {}", rect1.can_hold(&rect3)); diff --git a/listings/ch05-using-structs-to-structure-related-data/listing-05-15/src/main.rs b/listings/ch05-using-structs-to-structure-related-data/listing-05-15/src/main.rs index 5dd554ea1..e6a32723f 100644 --- a/listings/ch05-using-structs-to-structure-related-data/listing-05-15/src/main.rs +++ b/listings/ch05-using-structs-to-structure-related-data/listing-05-15/src/main.rs @@ -17,9 +17,18 @@ impl Rectangle { // ANCHOR_END: here fn main() { - let rect1 = Rectangle { width: 30, height: 50 }; - let rect2 = Rectangle { width: 10, height: 40 }; - let rect3 = Rectangle { width: 60, height: 45 }; + let rect1 = Rectangle { + width: 30, + height: 50, + }; + let rect2 = Rectangle { + width: 10, + height: 40, + }; + let rect3 = Rectangle { + width: 60, + height: 45, + }; println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2)); println!("Can rect1 hold rect3? {}", rect1.can_hold(&rect3)); diff --git a/listings/ch05-using-structs-to-structure-related-data/listing-05-16/src/main.rs b/listings/ch05-using-structs-to-structure-related-data/listing-05-16/src/main.rs index 89be55a01..a5d3f772a 100644 --- a/listings/ch05-using-structs-to-structure-related-data/listing-05-16/src/main.rs +++ b/listings/ch05-using-structs-to-structure-related-data/listing-05-16/src/main.rs @@ -18,11 +18,19 @@ impl Rectangle { } // ANCHOR_END: here - fn main() { - let rect1 = Rectangle { width: 30, height: 50 }; - let rect2 = Rectangle { width: 10, height: 40 }; - let rect3 = Rectangle { width: 60, height: 45 }; + let rect1 = Rectangle { + width: 30, + height: 50, + }; + let rect2 = Rectangle { + width: 10, + height: 40, + }; + let rect3 = Rectangle { + width: 60, + height: 45, + }; println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2)); println!("Can rect1 hold rect3? {}", rect1.can_hold(&rect3)); diff --git a/listings/ch05-using-structs-to-structure-related-data/no-listing-03-associated-functions/src/main.rs b/listings/ch05-using-structs-to-structure-related-data/no-listing-03-associated-functions/src/main.rs index 8b1d5abce..d5b1692a4 100644 --- a/listings/ch05-using-structs-to-structure-related-data/no-listing-03-associated-functions/src/main.rs +++ b/listings/ch05-using-structs-to-structure-related-data/no-listing-03-associated-functions/src/main.rs @@ -7,11 +7,14 @@ struct Rectangle { // ANCHOR: here impl Rectangle { fn square(size: u32) -> Rectangle { - Rectangle { width: size, height: size } + Rectangle { + width: size, + height: size, + } } } // ANCHOR_END: here fn main() { let sq = Rectangle::square(3); -} \ No newline at end of file +} diff --git a/listings/ch05-using-structs-to-structure-related-data/output-only-01-debug/output.txt b/listings/ch05-using-structs-to-structure-related-data/output-only-01-debug/output.txt index d1902d02a..0868bbdb6 100644 --- a/listings/ch05-using-structs-to-structure-related-data/output-only-01-debug/output.txt +++ b/listings/ch05-using-structs-to-structure-related-data/output-only-01-debug/output.txt @@ -1,14 +1,14 @@ $ cargo run Compiling structs v0.1.0 (file:///projects/structs) error[E0277]: `Rectangle` doesn't implement `std::fmt::Debug` - --> src/main.rs:9:31 - | -9 | println!("rect1 is {:?}", rect1); - | ^^^^^ `Rectangle` cannot be formatted using `{:?}` - | - = help: the trait `std::fmt::Debug` is not implemented for `Rectangle` - = note: add `#[derive(Debug)]` or manually implement `std::fmt::Debug` - = note: required by `std::fmt::Debug::fmt` + --> src/main.rs:12:31 + | +12 | println!("rect1 is {:?}", rect1); + | ^^^^^ `Rectangle` cannot be formatted using `{:?}` + | + = help: the trait `std::fmt::Debug` is not implemented for `Rectangle` + = note: add `#[derive(Debug)]` or manually implement `std::fmt::Debug` + = note: required by `std::fmt::Debug::fmt` error: aborting due to previous error diff --git a/listings/ch05-using-structs-to-structure-related-data/output-only-01-debug/src/main.rs b/listings/ch05-using-structs-to-structure-related-data/output-only-01-debug/src/main.rs index 1b3e5783c..019a357ab 100644 --- a/listings/ch05-using-structs-to-structure-related-data/output-only-01-debug/src/main.rs +++ b/listings/ch05-using-structs-to-structure-related-data/output-only-01-debug/src/main.rs @@ -4,7 +4,10 @@ struct Rectangle { } fn main() { - let rect1 = Rectangle { width: 30, height: 50 }; + let rect1 = Rectangle { + width: 30, + height: 50, + }; println!("rect1 is {:?}", rect1); } diff --git a/listings/ch05-using-structs-to-structure-related-data/output-only-02-pretty-debug/src/main.rs b/listings/ch05-using-structs-to-structure-related-data/output-only-02-pretty-debug/src/main.rs index 3cd1e8ceb..84e32aee4 100644 --- a/listings/ch05-using-structs-to-structure-related-data/output-only-02-pretty-debug/src/main.rs +++ b/listings/ch05-using-structs-to-structure-related-data/output-only-02-pretty-debug/src/main.rs @@ -5,7 +5,10 @@ struct Rectangle { } fn main() { - let rect1 = Rectangle { width: 30, height: 50 }; + let rect1 = Rectangle { + width: 30, + height: 50, + }; println!("rect1 is {:#?}", rect1); } diff --git a/listings/ch06-enums-and-pattern-matching/listing-06-03/src/main.rs b/listings/ch06-enums-and-pattern-matching/listing-06-03/src/main.rs index 6007e4ce4..93dce48cb 100644 --- a/listings/ch06-enums-and-pattern-matching/listing-06-03/src/main.rs +++ b/listings/ch06-enums-and-pattern-matching/listing-06-03/src/main.rs @@ -16,4 +16,4 @@ fn value_in_cents(coin: Coin) -> u8 { } // ANCHOR_END: here -fn main() {} \ No newline at end of file +fn main() {} diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-01-defining-enums/src/main.rs b/listings/ch06-enums-and-pattern-matching/no-listing-01-defining-enums/src/main.rs index 784119f7e..c631e56ba 100644 --- a/listings/ch06-enums-and-pattern-matching/no-listing-01-defining-enums/src/main.rs +++ b/listings/ch06-enums-and-pattern-matching/no-listing-01-defining-enums/src/main.rs @@ -18,5 +18,5 @@ fn main() { } // ANCHOR: fn -fn route(ip_kind: IpAddrKind) { } +fn route(ip_kind: IpAddrKind) {} // ANCHOR_END: fn diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-04-structs-similar-to-message-enum/src/main.rs b/listings/ch06-enums-and-pattern-matching/no-listing-04-structs-similar-to-message-enum/src/main.rs index a8e657ec1..df451be8b 100644 --- a/listings/ch06-enums-and-pattern-matching/no-listing-04-structs-similar-to-message-enum/src/main.rs +++ b/listings/ch06-enums-and-pattern-matching/no-listing-04-structs-similar-to-message-enum/src/main.rs @@ -6,7 +6,6 @@ struct MoveMessage { } struct WriteMessage(String); // tuple struct struct ChangeColorMessage(i32, i32, i32); // tuple struct -// ANCHOR_END: here + // ANCHOR_END: here -fn main() { -} +fn main() {} diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-08-match-arm-multiple-lines/src/main.rs b/listings/ch06-enums-and-pattern-matching/no-listing-08-match-arm-multiple-lines/src/main.rs index ac786e1f9..3f909dcaf 100644 --- a/listings/ch06-enums-and-pattern-matching/no-listing-08-match-arm-multiple-lines/src/main.rs +++ b/listings/ch06-enums-and-pattern-matching/no-listing-08-match-arm-multiple-lines/src/main.rs @@ -11,7 +11,7 @@ fn value_in_cents(coin: Coin) -> u8 { Coin::Penny => { println!("Lucky penny!"); 1 - }, + } Coin::Nickel => 5, Coin::Dime => 10, Coin::Quarter => 25, @@ -19,4 +19,4 @@ fn value_in_cents(coin: Coin) -> u8 { } // ANCHOR_END: here -fn main() {} \ No newline at end of file +fn main() {} diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-09-variable-in-pattern/src/main.rs b/listings/ch06-enums-and-pattern-matching/no-listing-09-variable-in-pattern/src/main.rs index 2cf799853..a4d500c11 100644 --- a/listings/ch06-enums-and-pattern-matching/no-listing-09-variable-in-pattern/src/main.rs +++ b/listings/ch06-enums-and-pattern-matching/no-listing-09-variable-in-pattern/src/main.rs @@ -21,7 +21,7 @@ fn value_in_cents(coin: Coin) -> u8 { Coin::Quarter(state) => { println!("State quarter from {:?}!", state); 25 - }, + } } } // ANCHOR_END: here diff --git a/listings/ch07-managing-growing-projects/listing-07-08/src/lib.rs b/listings/ch07-managing-growing-projects/listing-07-08/src/lib.rs index ee3cf5ecc..a789379ae 100644 --- a/listings/ch07-managing-growing-projects/listing-07-08/src/lib.rs +++ b/listings/ch07-managing-growing-projects/listing-07-08/src/lib.rs @@ -11,4 +11,4 @@ mod back_of_house { } // ANCHOR_END: here -fn main() {} \ No newline at end of file +fn main() {} diff --git a/listings/ch07-managing-growing-projects/listing-07-18/src/main.rs b/listings/ch07-managing-growing-projects/listing-07-18/src/main.rs index 0d23c3679..61a2023ff 100644 --- a/listings/ch07-managing-growing-projects/listing-07-18/src/main.rs +++ b/listings/ch07-managing-growing-projects/listing-07-18/src/main.rs @@ -1,5 +1,9 @@ -use std::{cmp::Ordering, io}; use rand::Rng; +// ANCHOR: here +// --snip-- +use std::{cmp::Ordering, io}; +// --snip-- +// ANCHOR_END: here fn main() { println!("Guess the number!"); @@ -12,11 +16,11 @@ fn main() { let mut guess = String::new(); - io::stdin().read_line(&mut guess) + io::stdin() + .read_line(&mut guess) .expect("Failed to read line"); - let guess: u32 = guess.trim().parse() - .expect("Please type a number!"); + let guess: u32 = guess.trim().parse().expect("Please type a number!"); println!("You guessed: {}", guess); diff --git a/listings/ch07-managing-growing-projects/no-listing-01-use-std-unnested/src/main.rs b/listings/ch07-managing-growing-projects/no-listing-01-use-std-unnested/src/main.rs index 5ccfb12a1..c25b0a52a 100644 --- a/listings/ch07-managing-growing-projects/no-listing-01-use-std-unnested/src/main.rs +++ b/listings/ch07-managing-growing-projects/no-listing-01-use-std-unnested/src/main.rs @@ -1,9 +1,10 @@ +use rand::Rng; // ANCHOR: here -use std::io; +// --snip-- use std::cmp::Ordering; +use std::io; // --snip-- // ANCHOR_END: here -use rand::Rng; fn main() { println!("Guess the number!"); @@ -16,7 +17,8 @@ fn main() { let mut guess = String::new(); - io::stdin().read_line(&mut guess) + io::stdin() + .read_line(&mut guess) .expect("Failed to read line"); println!("You guessed: {}", guess); diff --git a/listings/ch08-common-collections/listing-08-04/src/main.rs b/listings/ch08-common-collections/listing-08-04/src/main.rs index 7fd5a4907..abda2db66 100644 --- a/listings/ch08-common-collections/listing-08-04/src/main.rs +++ b/listings/ch08-common-collections/listing-08-04/src/main.rs @@ -4,7 +4,6 @@ fn main() { let v = vec![1, 2, 3, 4]; // do stuff with v - } // <- v goes out of scope and is freed here - // ANCHOR_END: here + // ANCHOR_END: here } diff --git a/listings/ch08-common-collections/listing-08-18/src/main.rs b/listings/ch08-common-collections/listing-08-18/src/main.rs index 8e55dc511..93939a69f 100644 --- a/listings/ch08-common-collections/listing-08-18/src/main.rs +++ b/listings/ch08-common-collections/listing-08-18/src/main.rs @@ -3,5 +3,5 @@ fn main() { let s1 = String::from("Hello, "); let s2 = String::from("world!"); let s3 = s1 + &s2; // note s1 has been moved here and can no longer be used - // ANCHOR_END: here + // ANCHOR_END: here } diff --git a/listings/ch08-common-collections/listing-08-21/src/main.rs b/listings/ch08-common-collections/listing-08-21/src/main.rs index ce51345a0..b21e0dee5 100644 --- a/listings/ch08-common-collections/listing-08-21/src/main.rs +++ b/listings/ch08-common-collections/listing-08-21/src/main.rs @@ -2,9 +2,10 @@ fn main() { // ANCHOR: here use std::collections::HashMap; - let teams = vec![String::from("Blue"), String::from("Yellow")]; + let teams = vec![String::from("Blue"), String::from("Yellow")]; let initial_scores = vec![10, 50]; - let scores: HashMap<_, _> = teams.iter().zip(initial_scores.iter()).collect(); + let scores: HashMap<_, _> = + teams.iter().zip(initial_scores.iter()).collect(); // ANCHOR_END: here } diff --git a/listings/ch09-error-handling/listing-09-04/output.txt b/listings/ch09-error-handling/listing-09-04/output.txt index 5bf2a522d..acc4f0c74 100644 --- a/listings/ch09-error-handling/listing-09-04/output.txt +++ b/listings/ch09-error-handling/listing-09-04/output.txt @@ -2,5 +2,5 @@ $ cargo run Compiling error-handling v0.1.0 (file:///projects/error-handling) Finished dev [unoptimized + debuginfo] target(s) in 0.73s Running `target/debug/error-handling` -thread 'main' panicked at 'Problem opening the file: Os { code: 2, kind: NotFound, message: "No such file or directory" }', src/main.rs:9:13 +thread 'main' panicked at 'Problem opening the file: Os { code: 2, kind: NotFound, message: "No such file or directory" }', src/main.rs:8:23 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace. diff --git a/listings/ch09-error-handling/listing-09-04/src/main.rs b/listings/ch09-error-handling/listing-09-04/src/main.rs index 65973a454..070fc3380 100644 --- a/listings/ch09-error-handling/listing-09-04/src/main.rs +++ b/listings/ch09-error-handling/listing-09-04/src/main.rs @@ -5,8 +5,6 @@ fn main() { let f = match f { Ok(file) => file, - Err(error) => { - panic!("Problem opening the file: {:?}", error) - }, + Err(error) => panic!("Problem opening the file: {:?}", error), }; } diff --git a/listings/ch09-error-handling/listing-09-05/src/main.rs b/listings/ch09-error-handling/listing-09-05/src/main.rs index d53952e3d..8c4f773b9 100644 --- a/listings/ch09-error-handling/listing-09-05/src/main.rs +++ b/listings/ch09-error-handling/listing-09-05/src/main.rs @@ -11,7 +11,9 @@ fn main() { Ok(fc) => fc, Err(e) => panic!("Problem creating the file: {:?}", e), }, - other_error => panic!("Problem opening the file: {:?}", other_error), + other_error => { + panic!("Problem opening the file: {:?}", other_error) + } }, }; } diff --git a/listings/ch09-error-handling/listing-09-06/src/main.rs b/listings/ch09-error-handling/listing-09-06/src/main.rs index 65b6156f2..437d858f4 100644 --- a/listings/ch09-error-handling/listing-09-06/src/main.rs +++ b/listings/ch09-error-handling/listing-09-06/src/main.rs @@ -1,7 +1,7 @@ // ANCHOR: here +use std::fs::File; use std::io; use std::io::Read; -use std::fs::File; fn read_username_from_file() -> Result { let f = File::open("hello.txt"); diff --git a/listings/ch09-error-handling/listing-09-07/src/main.rs b/listings/ch09-error-handling/listing-09-07/src/main.rs index 2fb154e32..b9f6172cb 100644 --- a/listings/ch09-error-handling/listing-09-07/src/main.rs +++ b/listings/ch09-error-handling/listing-09-07/src/main.rs @@ -1,7 +1,7 @@ // ANCHOR: here +use std::fs::File; use std::io; use std::io::Read; -use std::fs::File; fn read_username_from_file() -> Result { let mut f = File::open("hello.txt")?; diff --git a/listings/ch09-error-handling/listing-09-08/src/main.rs b/listings/ch09-error-handling/listing-09-08/src/main.rs index 930a8bf02..f36e4d021 100644 --- a/listings/ch09-error-handling/listing-09-08/src/main.rs +++ b/listings/ch09-error-handling/listing-09-08/src/main.rs @@ -1,7 +1,7 @@ // ANCHOR: here +use std::fs::File; use std::io; use std::io::Read; -use std::fs::File; fn read_username_from_file() -> Result { let mut s = String::new(); diff --git a/listings/ch09-error-handling/listing-09-09/src/main.rs b/listings/ch09-error-handling/listing-09-09/src/main.rs index f8a213bc7..4597dc2ee 100644 --- a/listings/ch09-error-handling/listing-09-09/src/main.rs +++ b/listings/ch09-error-handling/listing-09-09/src/main.rs @@ -1,6 +1,6 @@ // ANCHOR: here -use std::io; use std::fs; +use std::io; fn read_username_from_file() -> Result { fs::read_to_string("hello.txt") diff --git a/listings/ch09-error-handling/listing-09-10/src/main.rs b/listings/ch09-error-handling/listing-09-10/src/main.rs index be30ed72c..7448979a0 100644 --- a/listings/ch09-error-handling/listing-09-10/src/main.rs +++ b/listings/ch09-error-handling/listing-09-10/src/main.rs @@ -1,6 +1,6 @@ -use std::io; -use std::cmp::Ordering; use rand::Rng; +use std::cmp::Ordering; +use std::io; // ANCHOR: here pub struct Guess { @@ -13,9 +13,7 @@ impl Guess { panic!("Guess value must be between 1 and 100, got {}.", value); } - Guess { - value - } + Guess { value } } pub fn value(&self) -> i32 { @@ -34,7 +32,8 @@ fn main() { let mut guess = String::new(); - io::stdin().read_line(&mut guess) + io::stdin() + .read_line(&mut guess) .expect("Failed to read line"); let guess: i32 = match guess.trim().parse() { diff --git a/listings/ch09-error-handling/no-listing-09-guess-out-of-range/src/main.rs b/listings/ch09-error-handling/no-listing-09-guess-out-of-range/src/main.rs index 0fdd8460c..d89d719aa 100644 --- a/listings/ch09-error-handling/no-listing-09-guess-out-of-range/src/main.rs +++ b/listings/ch09-error-handling/no-listing-09-guess-out-of-range/src/main.rs @@ -1,6 +1,6 @@ -use std::io; -use std::cmp::Ordering; use rand::Rng; +use std::cmp::Ordering; +use std::io; fn main() { println!("Guess the number!"); @@ -16,7 +16,8 @@ fn main() { let mut guess = String::new(); - io::stdin().read_line(&mut guess) + io::stdin() + .read_line(&mut guess) .expect("Failed to read line"); // ANCHOR: here @@ -31,8 +32,8 @@ fn main() { } match guess.cmp(&secret_number) { - // --snip-- - // ANCHOR_END: here + // --snip-- + // ANCHOR_END: here Ordering::Less => println!("Too small!"), Ordering::Greater => println!("Too big!"), Ordering::Equal => { diff --git a/listings/ch10-generic-types-traits-and-lifetimes/listing-10-11/src/main.rs b/listings/ch10-generic-types-traits-and-lifetimes/listing-10-11/src/main.rs index 622d39b21..4a08d1a8d 100644 --- a/listings/ch10-generic-types-traits-and-lifetimes/listing-10-11/src/main.rs +++ b/listings/ch10-generic-types-traits-and-lifetimes/listing-10-11/src/main.rs @@ -14,7 +14,7 @@ impl Point { fn main() { let p1 = Point { x: 5, y: 10.4 }; - let p2 = Point { x: "Hello", y: 'c'}; + let p2 = Point { x: "Hello", y: 'c' }; let p3 = p1.mixup(p2); diff --git a/listings/ch10-generic-types-traits-and-lifetimes/listing-10-16/src/lib.rs b/listings/ch10-generic-types-traits-and-lifetimes/listing-10-16/src/lib.rs index 6009860db..669cc5fdc 100644 --- a/listings/ch10-generic-types-traits-and-lifetimes/listing-10-16/src/lib.rs +++ b/listings/ch10-generic-types-traits-and-lifetimes/listing-10-16/src/lib.rs @@ -7,10 +7,7 @@ struct Pair { impl Pair { fn new(x: T, y: T) -> Self { - Self { - x, - y, - } + Self { x, y } } } diff --git a/listings/ch10-generic-types-traits-and-lifetimes/listing-10-25/src/main.rs b/listings/ch10-generic-types-traits-and-lifetimes/listing-10-25/src/main.rs index 7d1387089..2937b194c 100644 --- a/listings/ch10-generic-types-traits-and-lifetimes/listing-10-25/src/main.rs +++ b/listings/ch10-generic-types-traits-and-lifetimes/listing-10-25/src/main.rs @@ -4,8 +4,8 @@ struct ImportantExcerpt<'a> { fn main() { let novel = String::from("Call me Ishmael. Some years ago..."); - let first_sentence = novel.split('.') - .next() - .expect("Could not find a '.'"); - let i = ImportantExcerpt { part: first_sentence }; + let first_sentence = novel.split('.').next().expect("Could not find a '.'"); + let i = ImportantExcerpt { + part: first_sentence, + }; } diff --git a/listings/ch10-generic-types-traits-and-lifetimes/no-listing-01-calling-trait-method/src/main.rs b/listings/ch10-generic-types-traits-and-lifetimes/no-listing-01-calling-trait-method/src/main.rs index 94fb22c6f..466dc4d59 100644 --- a/listings/ch10-generic-types-traits-and-lifetimes/no-listing-01-calling-trait-method/src/main.rs +++ b/listings/ch10-generic-types-traits-and-lifetimes/no-listing-01-calling-trait-method/src/main.rs @@ -1,10 +1,12 @@ -use chapter10::{self, Tweet, Summary}; +use chapter10::{self, Summary, Tweet}; fn main() { // ANCHOR: here let tweet = Tweet { username: String::from("horse_ebooks"), - content: String::from("of course, as you probably already know, people"), + content: String::from( + "of course, as you probably already know, people", + ), reply: false, retweet: false, }; diff --git a/listings/ch10-generic-types-traits-and-lifetimes/no-listing-02-calling-default-impl/src/main.rs b/listings/ch10-generic-types-traits-and-lifetimes/no-listing-02-calling-default-impl/src/main.rs index de599f68e..44c9c64ea 100644 --- a/listings/ch10-generic-types-traits-and-lifetimes/no-listing-02-calling-default-impl/src/main.rs +++ b/listings/ch10-generic-types-traits-and-lifetimes/no-listing-02-calling-default-impl/src/main.rs @@ -6,8 +6,10 @@ fn main() { 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."), + content: String::from( + "The Pittsburgh Penguins once again are the best \ + hockey team in the NHL.", + ), }; println!("New article available! {}", article.summarize()); diff --git a/listings/ch10-generic-types-traits-and-lifetimes/no-listing-03-default-impl-calls-other-methods/src/main.rs b/listings/ch10-generic-types-traits-and-lifetimes/no-listing-03-default-impl-calls-other-methods/src/main.rs index 94fb22c6f..466dc4d59 100644 --- a/listings/ch10-generic-types-traits-and-lifetimes/no-listing-03-default-impl-calls-other-methods/src/main.rs +++ b/listings/ch10-generic-types-traits-and-lifetimes/no-listing-03-default-impl-calls-other-methods/src/main.rs @@ -1,10 +1,12 @@ -use chapter10::{self, Tweet, Summary}; +use chapter10::{self, Summary, Tweet}; fn main() { // ANCHOR: here let tweet = Tweet { username: String::from("horse_ebooks"), - content: String::from("of course, as you probably already know, people"), + content: String::from( + "of course, as you probably already know, people", + ), reply: false, retweet: false, }; diff --git a/listings/ch10-generic-types-traits-and-lifetimes/no-listing-05-returning-impl-trait/src/lib.rs b/listings/ch10-generic-types-traits-and-lifetimes/no-listing-05-returning-impl-trait/src/lib.rs index 15d602c64..a611fce38 100644 --- a/listings/ch10-generic-types-traits-and-lifetimes/no-listing-05-returning-impl-trait/src/lib.rs +++ b/listings/ch10-generic-types-traits-and-lifetimes/no-listing-05-returning-impl-trait/src/lib.rs @@ -32,7 +32,9 @@ impl Summary for Tweet { fn returns_summarizable() -> impl Summary { Tweet { username: String::from("horse_ebooks"), - content: String::from("of course, as you probably already know, people"), + content: String::from( + "of course, as you probably already know, people", + ), reply: false, retweet: false, } diff --git a/listings/ch10-generic-types-traits-and-lifetimes/no-listing-06-impl-trait-returns-one-type/src/lib.rs b/listings/ch10-generic-types-traits-and-lifetimes/no-listing-06-impl-trait-returns-one-type/src/lib.rs index c229f9494..7cd81d4c3 100644 --- a/listings/ch10-generic-types-traits-and-lifetimes/no-listing-06-impl-trait-returns-one-type/src/lib.rs +++ b/listings/ch10-generic-types-traits-and-lifetimes/no-listing-06-impl-trait-returns-one-type/src/lib.rs @@ -32,16 +32,22 @@ impl Summary for Tweet { fn returns_summarizable(switch: bool) -> impl Summary { if switch { NewsArticle { - headline: String::from("Penguins win the Stanley Cup Championship!"), + 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."), + 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"), + content: String::from( + "of course, as you probably already know, people", + ), reply: false, retweet: false, } diff --git a/listings/ch10-generic-types-traits-and-lifetimes/no-listing-10-lifetimes-on-methods/src/main.rs b/listings/ch10-generic-types-traits-and-lifetimes/no-listing-10-lifetimes-on-methods/src/main.rs index b841a03e2..32ad530b5 100644 --- a/listings/ch10-generic-types-traits-and-lifetimes/no-listing-10-lifetimes-on-methods/src/main.rs +++ b/listings/ch10-generic-types-traits-and-lifetimes/no-listing-10-lifetimes-on-methods/src/main.rs @@ -21,8 +21,8 @@ impl<'a> ImportantExcerpt<'a> { fn main() { let novel = String::from("Call me Ishmael. Some years ago..."); - let first_sentence = novel.split('.') - .next() - .expect("Could not find a '.'"); - let i = ImportantExcerpt { part: first_sentence }; + let first_sentence = novel.split('.').next().expect("Could not find a '.'"); + let i = ImportantExcerpt { + part: first_sentence, + }; } diff --git a/listings/ch10-generic-types-traits-and-lifetimes/no-listing-11-generics-traits-and-lifetimes/src/main.rs b/listings/ch10-generic-types-traits-and-lifetimes/no-listing-11-generics-traits-and-lifetimes/src/main.rs index 868752337..cfafa9a6d 100644 --- a/listings/ch10-generic-types-traits-and-lifetimes/no-listing-11-generics-traits-and-lifetimes/src/main.rs +++ b/listings/ch10-generic-types-traits-and-lifetimes/no-listing-11-generics-traits-and-lifetimes/src/main.rs @@ -2,15 +2,24 @@ fn main() { let string1 = String::from("abcd"); let string2 = "xyz"; - let result = longest_with_an_announcement(string1.as_str(), string2, "Today is someone's birthday!"); + let result = longest_with_an_announcement( + string1.as_str(), + string2, + "Today is someone's birthday!", + ); println!("The longest string is {}", result); } // ANCHOR: here use std::fmt::Display; -fn longest_with_an_announcement<'a, T>(x: &'a str, y: &'a str, ann: T) -> &'a str - where T: Display +fn longest_with_an_announcement<'a, T>( + x: &'a str, + y: &'a str, + ann: T, +) -> &'a str +where + T: Display, { println!("Announcement! {}", ann); if x.len() > y.len() { diff --git a/listings/ch11-writing-automated-tests/listing-11-06/src/lib.rs b/listings/ch11-writing-automated-tests/listing-11-06/src/lib.rs index f8a1f9219..a02395313 100644 --- a/listings/ch11-writing-automated-tests/listing-11-06/src/lib.rs +++ b/listings/ch11-writing-automated-tests/listing-11-06/src/lib.rs @@ -17,8 +17,14 @@ mod tests { #[test] fn larger_can_hold_smaller() { - let larger = Rectangle { width: 8, height: 7 }; - let smaller = Rectangle { width: 5, height: 1 }; + let larger = Rectangle { + width: 8, + height: 7, + }; + let smaller = Rectangle { + width: 5, + height: 1, + }; assert!(larger.can_hold(&smaller)); } diff --git a/listings/ch11-writing-automated-tests/listing-11-08/src/lib.rs b/listings/ch11-writing-automated-tests/listing-11-08/src/lib.rs index 68e740a3d..b41730213 100644 --- a/listings/ch11-writing-automated-tests/listing-11-08/src/lib.rs +++ b/listings/ch11-writing-automated-tests/listing-11-08/src/lib.rs @@ -9,9 +9,7 @@ impl Guess { panic!("Guess value must be between 1 and 100, got {}.", value); } - Guess { - value - } + Guess { value } } } diff --git a/listings/ch11-writing-automated-tests/listing-11-09/src/lib.rs b/listings/ch11-writing-automated-tests/listing-11-09/src/lib.rs index d0069a89e..ffa543bfb 100644 --- a/listings/ch11-writing-automated-tests/listing-11-09/src/lib.rs +++ b/listings/ch11-writing-automated-tests/listing-11-09/src/lib.rs @@ -7,16 +7,18 @@ pub struct Guess { impl Guess { pub fn new(value: i32) -> Guess { if value < 1 { - panic!("Guess value must be greater than or equal to 1, got {}.", - value); + panic!( + "Guess value must be greater than or equal to 1, got {}.", + value + ); } else if value > 100 { - panic!("Guess value must be less than or equal to 100, got {}.", - value); + panic!( + "Guess value must be less than or equal to 100, got {}.", + value + ); } - Guess { - value - } + Guess { value } } } diff --git a/listings/ch11-writing-automated-tests/listing-11-13/src/lib.rs b/listings/ch11-writing-automated-tests/listing-11-13/src/lib.rs index f36aba399..c3961b1f6 100644 --- a/listings/ch11-writing-automated-tests/listing-11-13/src/lib.rs +++ b/listings/ch11-writing-automated-tests/listing-11-13/src/lib.rs @@ -14,4 +14,4 @@ mod tests { fn internal() { assert_eq!(4, internal_adder(2, 2)); } -} \ No newline at end of file +} diff --git a/listings/ch11-writing-automated-tests/no-listing-02-adding-another-rectangle-test/src/lib.rs b/listings/ch11-writing-automated-tests/no-listing-02-adding-another-rectangle-test/src/lib.rs index c20a7315f..338ad0936 100644 --- a/listings/ch11-writing-automated-tests/no-listing-02-adding-another-rectangle-test/src/lib.rs +++ b/listings/ch11-writing-automated-tests/no-listing-02-adding-another-rectangle-test/src/lib.rs @@ -19,8 +19,14 @@ mod tests { fn larger_can_hold_smaller() { // --snip-- // ANCHOR_END: here - let larger = Rectangle { width: 8, height: 7 }; - let smaller = Rectangle { width: 5, height: 1 }; + let larger = Rectangle { + width: 8, + height: 7, + }; + let smaller = Rectangle { + width: 5, + height: 1, + }; assert!(larger.can_hold(&smaller)); // ANCHOR: here @@ -28,8 +34,14 @@ mod tests { #[test] fn smaller_cannot_hold_larger() { - let larger = Rectangle { width: 8, height: 7 }; - let smaller = Rectangle { width: 5, height: 1 }; + let larger = Rectangle { + width: 8, + height: 7, + }; + let smaller = Rectangle { + width: 5, + height: 1, + }; assert!(!smaller.can_hold(&larger)); } diff --git a/listings/ch11-writing-automated-tests/no-listing-03-introducing-a-bug/output.txt b/listings/ch11-writing-automated-tests/no-listing-03-introducing-a-bug/output.txt index 7207126a9..a96eaf481 100644 --- a/listings/ch11-writing-automated-tests/no-listing-03-introducing-a-bug/output.txt +++ b/listings/ch11-writing-automated-tests/no-listing-03-introducing-a-bug/output.txt @@ -10,7 +10,7 @@ test tests::smaller_cannot_hold_larger ... ok failures: ---- tests::larger_can_hold_smaller stdout ---- -thread 'main' panicked at 'assertion failed: larger.can_hold(&smaller)', src/lib.rs:22:9 +thread 'main' panicked at 'assertion failed: larger.can_hold(&smaller)', src/lib.rs:28:9 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace. diff --git a/listings/ch11-writing-automated-tests/no-listing-03-introducing-a-bug/src/lib.rs b/listings/ch11-writing-automated-tests/no-listing-03-introducing-a-bug/src/lib.rs index 6e065c659..33a2dd0ac 100644 --- a/listings/ch11-writing-automated-tests/no-listing-03-introducing-a-bug/src/lib.rs +++ b/listings/ch11-writing-automated-tests/no-listing-03-introducing-a-bug/src/lib.rs @@ -19,16 +19,28 @@ mod tests { #[test] fn larger_can_hold_smaller() { - let larger = Rectangle { width: 8, height: 7 }; - let smaller = Rectangle { width: 5, height: 1 }; + let larger = Rectangle { + width: 8, + height: 7, + }; + let smaller = Rectangle { + width: 5, + height: 1, + }; assert!(larger.can_hold(&smaller)); } #[test] fn smaller_cannot_hold_larger() { - let larger = Rectangle { width: 8, height: 7 }; - let smaller = Rectangle { width: 5, height: 1 }; + let larger = Rectangle { + width: 8, + height: 7, + }; + let smaller = Rectangle { + width: 5, + height: 1, + }; assert!(!smaller.can_hold(&larger)); } diff --git a/listings/ch11-writing-automated-tests/no-listing-07-custom-failure-message/src/lib.rs b/listings/ch11-writing-automated-tests/no-listing-07-custom-failure-message/src/lib.rs index bf42d36e3..519c7a4c6 100644 --- a/listings/ch11-writing-automated-tests/no-listing-07-custom-failure-message/src/lib.rs +++ b/listings/ch11-writing-automated-tests/no-listing-07-custom-failure-message/src/lib.rs @@ -12,7 +12,8 @@ mod tests { let result = greeting("Carol"); assert!( result.contains("Carol"), - "Greeting did not contain name, value was `{}`", result + "Greeting did not contain name, value was `{}`", + result ); } // ANCHOR_END: here diff --git a/listings/ch11-writing-automated-tests/no-listing-08-guess-with-bug/src/lib.rs b/listings/ch11-writing-automated-tests/no-listing-08-guess-with-bug/src/lib.rs index 54096143c..269c73cef 100644 --- a/listings/ch11-writing-automated-tests/no-listing-08-guess-with-bug/src/lib.rs +++ b/listings/ch11-writing-automated-tests/no-listing-08-guess-with-bug/src/lib.rs @@ -6,13 +6,11 @@ pub struct Guess { // --snip-- impl Guess { pub fn new(value: i32) -> Guess { - if value < 1 { + if value < 1 { panic!("Guess value must be between 1 and 100, got {}.", value); } - Guess { - value - } + Guess { value } } } // ANCHOR_END: here diff --git a/listings/ch11-writing-automated-tests/no-listing-09-guess-with-panic-msg-bug/output.txt b/listings/ch11-writing-automated-tests/no-listing-09-guess-with-panic-msg-bug/output.txt index 6cb5ab618..d4e8ec58c 100644 --- a/listings/ch11-writing-automated-tests/no-listing-09-guess-with-panic-msg-bug/output.txt +++ b/listings/ch11-writing-automated-tests/no-listing-09-guess-with-panic-msg-bug/output.txt @@ -9,7 +9,7 @@ test tests::greater_than_100 ... FAILED failures: ---- tests::greater_than_100 stdout ---- -thread 'main' panicked at 'Guess value must be greater than or equal to 1, got 200.', src/lib.rs:11:13 +thread 'main' panicked at 'Guess value must be greater than or equal to 1, got 200.', src/lib.rs:13:13 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace. note: panic did not include expected string 'Guess value must be less than or equal to 100' diff --git a/listings/ch11-writing-automated-tests/no-listing-09-guess-with-panic-msg-bug/src/lib.rs b/listings/ch11-writing-automated-tests/no-listing-09-guess-with-panic-msg-bug/src/lib.rs index 59e818ccc..21650040d 100644 --- a/listings/ch11-writing-automated-tests/no-listing-09-guess-with-panic-msg-bug/src/lib.rs +++ b/listings/ch11-writing-automated-tests/no-listing-09-guess-with-panic-msg-bug/src/lib.rs @@ -6,17 +6,19 @@ impl Guess { pub fn new(value: i32) -> Guess { // ANCHOR: here if value < 1 { - panic!("Guess value must be less than or equal to 100, got {}.", - value); + panic!( + "Guess value must be less than or equal to 100, got {}.", + value + ); } else if value > 100 { - panic!("Guess value must be greater than or equal to 1, got {}.", - value); + panic!( + "Guess value must be greater than or equal to 1, got {}.", + value + ); } // ANCHOR_END: here - Guess { - value - } + Guess { value } } } diff --git a/listings/ch11-writing-automated-tests/no-listing-12-shared-test-code-problem/src/lib.rs b/listings/ch11-writing-automated-tests/no-listing-12-shared-test-code-problem/src/lib.rs index f36aba399..c3961b1f6 100644 --- a/listings/ch11-writing-automated-tests/no-listing-12-shared-test-code-problem/src/lib.rs +++ b/listings/ch11-writing-automated-tests/no-listing-12-shared-test-code-problem/src/lib.rs @@ -14,4 +14,4 @@ mod tests { fn internal() { assert_eq!(4, internal_adder(2, 2)); } -} \ No newline at end of file +} diff --git a/listings/ch11-writing-automated-tests/no-listing-13-fix-shared-test-code-problem/src/lib.rs b/listings/ch11-writing-automated-tests/no-listing-13-fix-shared-test-code-problem/src/lib.rs index f36aba399..c3961b1f6 100644 --- a/listings/ch11-writing-automated-tests/no-listing-13-fix-shared-test-code-problem/src/lib.rs +++ b/listings/ch11-writing-automated-tests/no-listing-13-fix-shared-test-code-problem/src/lib.rs @@ -14,4 +14,4 @@ mod tests { fn internal() { assert_eq!(4, internal_adder(2, 2)); } -} \ No newline at end of file +} diff --git a/listings/ch11-writing-automated-tests/output-only-05-single-integration/src/lib.rs b/listings/ch11-writing-automated-tests/output-only-05-single-integration/src/lib.rs index f36aba399..c3961b1f6 100644 --- a/listings/ch11-writing-automated-tests/output-only-05-single-integration/src/lib.rs +++ b/listings/ch11-writing-automated-tests/output-only-05-single-integration/src/lib.rs @@ -14,4 +14,4 @@ mod tests { fn internal() { assert_eq!(4, internal_adder(2, 2)); } -} \ No newline at end of file +} diff --git a/listings/ch12-an-io-project/listing-12-08/src/main.rs b/listings/ch12-an-io-project/listing-12-08/src/main.rs index 462a16a05..dddf10bd4 100644 --- a/listings/ch12-an-io-project/listing-12-08/src/main.rs +++ b/listings/ch12-an-io-project/listing-12-08/src/main.rs @@ -22,7 +22,7 @@ struct Config { impl Config { // ANCHOR: here -// --snip-- + // --snip-- fn new(args: &[String]) -> Config { if args.len() < 3 { panic!("not enough arguments"); diff --git a/listings/ch12-an-io-project/listing-12-15/src/lib.rs b/listings/ch12-an-io-project/listing-12-15/src/lib.rs index 74a92643a..22ec38178 100644 --- a/listings/ch12-an-io-project/listing-12-15/src/lib.rs +++ b/listings/ch12-an-io-project/listing-12-15/src/lib.rs @@ -38,10 +38,7 @@ Rust: safe, fast, productive. Pick three."; - assert_eq!( - vec!["safe, fast, productive."], - search(query, contents) - ); + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); } } // ANCHOR_END: here diff --git a/listings/ch12-an-io-project/listing-12-16/src/lib.rs b/listings/ch12-an-io-project/listing-12-16/src/lib.rs index a1d09a318..2fc5b4f72 100644 --- a/listings/ch12-an-io-project/listing-12-16/src/lib.rs +++ b/listings/ch12-an-io-project/listing-12-16/src/lib.rs @@ -43,10 +43,7 @@ Rust: safe, fast, productive. Pick three."; - assert_eq!( - vec!["safe, fast, productive."], - search(query, contents) - ); + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); } } diff --git a/listings/ch12-an-io-project/listing-12-17/src/lib.rs b/listings/ch12-an-io-project/listing-12-17/src/lib.rs index 185136729..0b094ce28 100644 --- a/listings/ch12-an-io-project/listing-12-17/src/lib.rs +++ b/listings/ch12-an-io-project/listing-12-17/src/lib.rs @@ -45,9 +45,6 @@ Rust: safe, fast, productive. Pick three."; - assert_eq!( - vec!["safe, fast, productive."], - search(query, contents) - ); + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); } } diff --git a/listings/ch12-an-io-project/listing-12-18/src/lib.rs b/listings/ch12-an-io-project/listing-12-18/src/lib.rs index 94c59b79a..c9d1f529d 100644 --- a/listings/ch12-an-io-project/listing-12-18/src/lib.rs +++ b/listings/ch12-an-io-project/listing-12-18/src/lib.rs @@ -47,9 +47,6 @@ Rust: safe, fast, productive. Pick three."; - assert_eq!( - vec!["safe, fast, productive."], - search(query, contents) - ); + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); } } diff --git a/listings/ch12-an-io-project/listing-12-19/src/lib.rs b/listings/ch12-an-io-project/listing-12-19/src/lib.rs index 2e09856c3..de8897b8d 100644 --- a/listings/ch12-an-io-project/listing-12-19/src/lib.rs +++ b/listings/ch12-an-io-project/listing-12-19/src/lib.rs @@ -53,9 +53,6 @@ Rust: safe, fast, productive. Pick three."; - assert_eq!( - vec!["safe, fast, productive."], - search(query, contents) - ); + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); } } diff --git a/listings/ch12-an-io-project/listing-12-20/src/lib.rs b/listings/ch12-an-io-project/listing-12-20/src/lib.rs index 6ed35e098..52ba2bd51 100644 --- a/listings/ch12-an-io-project/listing-12-20/src/lib.rs +++ b/listings/ch12-an-io-project/listing-12-20/src/lib.rs @@ -55,10 +55,7 @@ safe, fast, productive. Pick three. Duct tape."; - assert_eq!( - vec!["safe, fast, productive."], - search(query, contents) - ); + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); } #[test] diff --git a/listings/ch12-an-io-project/listing-12-21/src/lib.rs b/listings/ch12-an-io-project/listing-12-21/src/lib.rs index 58fbf4696..873c8b53d 100644 --- a/listings/ch12-an-io-project/listing-12-21/src/lib.rs +++ b/listings/ch12-an-io-project/listing-12-21/src/lib.rs @@ -42,7 +42,10 @@ pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { } // ANCHOR: here -pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { +pub fn search_case_insensitive<'a>( + query: &str, + contents: &'a str, +) -> Vec<&'a str> { let query = query.to_lowercase(); let mut results = Vec::new(); @@ -69,10 +72,7 @@ safe, fast, productive. Pick three. Duct tape."; - assert_eq!( - vec!["safe, fast, productive."], - search(query, contents) - ); + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); } #[test] diff --git a/listings/ch12-an-io-project/listing-12-22/src/lib.rs b/listings/ch12-an-io-project/listing-12-22/src/lib.rs index 667c003af..2df82b7e2 100644 --- a/listings/ch12-an-io-project/listing-12-22/src/lib.rs +++ b/listings/ch12-an-io-project/listing-12-22/src/lib.rs @@ -52,7 +52,10 @@ pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { results } -pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { +pub fn search_case_insensitive<'a>( + query: &str, + contents: &'a str, +) -> Vec<&'a str> { let query = query.to_lowercase(); let mut results = Vec::new(); @@ -78,10 +81,7 @@ safe, fast, productive. Pick three. Duct tape."; - assert_eq!( - vec!["safe, fast, productive."], - search(query, contents) - ); + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); } #[test] diff --git a/listings/ch12-an-io-project/listing-12-23/src/lib.rs b/listings/ch12-an-io-project/listing-12-23/src/lib.rs index 920d67470..e6dc51a0e 100644 --- a/listings/ch12-an-io-project/listing-12-23/src/lib.rs +++ b/listings/ch12-an-io-project/listing-12-23/src/lib.rs @@ -24,7 +24,11 @@ impl Config { let case_sensitive = env::var("CASE_INSENSITIVE").is_err(); - Ok(Config { query, filename, case_sensitive }) + Ok(Config { + query, + filename, + case_sensitive, + }) } } // ANCHOR_END: here @@ -57,7 +61,10 @@ pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { results } -pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { +pub fn search_case_insensitive<'a>( + query: &str, + contents: &'a str, +) -> Vec<&'a str> { let query = query.to_lowercase(); let mut results = Vec::new(); @@ -83,10 +90,7 @@ safe, fast, productive. Pick three. Duct tape."; - assert_eq!( - vec!["safe, fast, productive."], - search(query, contents) - ); + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); } #[test] diff --git a/listings/ch12-an-io-project/listing-12-24/src/lib.rs b/listings/ch12-an-io-project/listing-12-24/src/lib.rs index 10792052e..fe1dccf4e 100644 --- a/listings/ch12-an-io-project/listing-12-24/src/lib.rs +++ b/listings/ch12-an-io-project/listing-12-24/src/lib.rs @@ -19,7 +19,11 @@ impl Config { let case_sensitive = env::var("CASE_INSENSITIVE").is_err(); - Ok(Config { query, filename, case_sensitive }) + Ok(Config { + query, + filename, + case_sensitive, + }) } } @@ -51,7 +55,10 @@ pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { results } -pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { +pub fn search_case_insensitive<'a>( + query: &str, + contents: &'a str, +) -> Vec<&'a str> { let query = query.to_lowercase(); let mut results = Vec::new(); @@ -77,10 +84,7 @@ safe, fast, productive. Pick three. Duct tape."; - assert_eq!( - vec!["safe, fast, productive."], - search(query, contents) - ); + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); } #[test] diff --git a/listings/ch12-an-io-project/no-listing-01-handling-errors-in-main/src/main.rs b/listings/ch12-an-io-project/no-listing-01-handling-errors-in-main/src/main.rs index 88df1eb21..44bcfeab3 100644 --- a/listings/ch12-an-io-project/no-listing-01-handling-errors-in-main/src/main.rs +++ b/listings/ch12-an-io-project/no-listing-01-handling-errors-in-main/src/main.rs @@ -1,7 +1,7 @@ use std::env; +use std::error::Error; use std::fs; use std::process; -use std::error::Error; // ANCHOR: here fn main() { diff --git a/listings/ch12-an-io-project/no-listing-02-using-search-in-run/src/lib.rs b/listings/ch12-an-io-project/no-listing-02-using-search-in-run/src/lib.rs index 794798272..ede4eb49a 100644 --- a/listings/ch12-an-io-project/no-listing-02-using-search-in-run/src/lib.rs +++ b/listings/ch12-an-io-project/no-listing-02-using-search-in-run/src/lib.rs @@ -55,9 +55,6 @@ Rust: safe, fast, productive. Pick three."; - assert_eq!( - vec!["safe, fast, productive."], - search(query, contents) - ); + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); } } diff --git a/listings/ch12-an-io-project/output-only-02-missing-lifetimes/src/lib.rs b/listings/ch12-an-io-project/output-only-02-missing-lifetimes/src/lib.rs index 8a12ac2f2..c60405c21 100644 --- a/listings/ch12-an-io-project/output-only-02-missing-lifetimes/src/lib.rs +++ b/listings/ch12-an-io-project/output-only-02-missing-lifetimes/src/lib.rs @@ -43,10 +43,7 @@ Rust: safe, fast, productive. Pick three."; - assert_eq!( - vec!["safe, fast, productive."], - search(query, contents) - ); + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); } } diff --git a/listings/ch12-an-io-project/output-only-03-multiple-matches/src/lib.rs b/listings/ch12-an-io-project/output-only-03-multiple-matches/src/lib.rs index 794798272..ede4eb49a 100644 --- a/listings/ch12-an-io-project/output-only-03-multiple-matches/src/lib.rs +++ b/listings/ch12-an-io-project/output-only-03-multiple-matches/src/lib.rs @@ -55,9 +55,6 @@ Rust: safe, fast, productive. Pick three."; - assert_eq!( - vec!["safe, fast, productive."], - search(query, contents) - ); + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); } } diff --git a/listings/ch12-an-io-project/output-only-04-no-matches/src/lib.rs b/listings/ch12-an-io-project/output-only-04-no-matches/src/lib.rs index 794798272..ede4eb49a 100644 --- a/listings/ch12-an-io-project/output-only-04-no-matches/src/lib.rs +++ b/listings/ch12-an-io-project/output-only-04-no-matches/src/lib.rs @@ -55,9 +55,6 @@ Rust: safe, fast, productive. Pick three."; - assert_eq!( - vec!["safe, fast, productive."], - search(query, contents) - ); + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); } } diff --git a/listings/ch13-functional-features/listing-12-23-reproduced/src/lib.rs b/listings/ch13-functional-features/listing-12-23-reproduced/src/lib.rs index 936aeac21..5397d7f61 100644 --- a/listings/ch13-functional-features/listing-12-23-reproduced/src/lib.rs +++ b/listings/ch13-functional-features/listing-12-23-reproduced/src/lib.rs @@ -20,7 +20,11 @@ impl Config { let case_sensitive = env::var("CASE_INSENSITIVE").is_err(); - Ok(Config { query, filename, case_sensitive }) + Ok(Config { + query, + filename, + case_sensitive, + }) } } // ANCHOR_END: ch13 @@ -53,7 +57,10 @@ pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { results } -pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { +pub fn search_case_insensitive<'a>( + query: &str, + contents: &'a str, +) -> Vec<&'a str> { let query = query.to_lowercase(); let mut results = Vec::new(); @@ -79,10 +86,7 @@ safe, fast, productive. Pick three. Duct tape."; - assert_eq!( - vec!["safe, fast, productive."], - search(query, contents) - ); + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); } #[test] diff --git a/listings/ch13-functional-features/listing-12-24-reproduced/src/lib.rs b/listings/ch13-functional-features/listing-12-24-reproduced/src/lib.rs index 10792052e..fe1dccf4e 100644 --- a/listings/ch13-functional-features/listing-12-24-reproduced/src/lib.rs +++ b/listings/ch13-functional-features/listing-12-24-reproduced/src/lib.rs @@ -19,7 +19,11 @@ impl Config { let case_sensitive = env::var("CASE_INSENSITIVE").is_err(); - Ok(Config { query, filename, case_sensitive }) + Ok(Config { + query, + filename, + case_sensitive, + }) } } @@ -51,7 +55,10 @@ pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { results } -pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { +pub fn search_case_insensitive<'a>( + query: &str, + contents: &'a str, +) -> Vec<&'a str> { let query = query.to_lowercase(); let mut results = Vec::new(); @@ -77,10 +84,7 @@ safe, fast, productive. Pick three. Duct tape."; - assert_eq!( - vec!["safe, fast, productive."], - search(query, contents) - ); + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); } #[test] diff --git a/listings/ch13-functional-features/listing-13-02/src/main.rs b/listings/ch13-functional-features/listing-13-02/src/main.rs index 308272648..96d06c77d 100644 --- a/listings/ch13-functional-features/listing-13-02/src/main.rs +++ b/listings/ch13-functional-features/listing-13-02/src/main.rs @@ -14,9 +14,6 @@ fn main() { let simulated_user_specified_value = 10; let simulated_random_number = 7; - generate_workout( - simulated_user_specified_value, - simulated_random_number - ); + generate_workout(simulated_user_specified_value, simulated_random_number); } // ANCHOR_END: here diff --git a/listings/ch13-functional-features/listing-13-03/src/main.rs b/listings/ch13-functional-features/listing-13-03/src/main.rs index 509f98cb8..d43c9b211 100644 --- a/listings/ch13-functional-features/listing-13-03/src/main.rs +++ b/listings/ch13-functional-features/listing-13-03/src/main.rs @@ -35,8 +35,5 @@ fn main() { let simulated_user_specified_value = 10; let simulated_random_number = 7; - generate_workout( - simulated_user_specified_value, - simulated_random_number - ); + generate_workout(simulated_user_specified_value, simulated_random_number); } diff --git a/listings/ch13-functional-features/listing-13-04/src/main.rs b/listings/ch13-functional-features/listing-13-04/src/main.rs index 95deed3fb..fabe0fb01 100644 --- a/listings/ch13-functional-features/listing-13-04/src/main.rs +++ b/listings/ch13-functional-features/listing-13-04/src/main.rs @@ -9,26 +9,16 @@ fn simulated_expensive_calculation(intensity: u32) -> u32 { // ANCHOR: here fn generate_workout(intensity: u32, random_number: u32) { - let expensive_result = - simulated_expensive_calculation(intensity); + let expensive_result = simulated_expensive_calculation(intensity); if intensity < 25 { - println!( - "Today, do {} pushups!", - expensive_result - ); - println!( - "Next, do {} situps!", - expensive_result - ); + println!("Today, do {} pushups!", expensive_result); + println!("Next, do {} situps!", expensive_result); } else { if random_number == 3 { println!("Take a break today! Remember to stay hydrated!"); } else { - println!( - "Today, run for {} minutes!", - expensive_result - ); + println!("Today, run for {} minutes!", expensive_result); } } } @@ -38,8 +28,5 @@ fn main() { let simulated_user_specified_value = 10; let simulated_random_number = 7; - generate_workout( - simulated_user_specified_value, - simulated_random_number - ); + generate_workout(simulated_user_specified_value, simulated_random_number); } diff --git a/listings/ch13-functional-features/listing-13-05/src/main.rs b/listings/ch13-functional-features/listing-13-05/src/main.rs index c30eff590..6984a27a0 100644 --- a/listings/ch13-functional-features/listing-13-05/src/main.rs +++ b/listings/ch13-functional-features/listing-13-05/src/main.rs @@ -11,14 +11,8 @@ fn generate_workout(intensity: u32, random_number: u32) { // ANCHOR_END: here if intensity < 25 { - println!( - "Today, do {} pushups!", - expensive_closure(intensity) - ); - println!( - "Next, do {} situps!", - expensive_closure(intensity) - ); + println!("Today, do {} pushups!", expensive_closure(intensity)); + println!("Next, do {} situps!", expensive_closure(intensity)); } else { if random_number == 3 { println!("Take a break today! Remember to stay hydrated!"); @@ -35,8 +29,5 @@ fn main() { let simulated_user_specified_value = 10; let simulated_random_number = 7; - generate_workout( - simulated_user_specified_value, - simulated_random_number - ); + generate_workout(simulated_user_specified_value, simulated_random_number); } diff --git a/listings/ch13-functional-features/listing-13-06/src/main.rs b/listings/ch13-functional-features/listing-13-06/src/main.rs index 66c2e770e..8850e58a7 100644 --- a/listings/ch13-functional-features/listing-13-06/src/main.rs +++ b/listings/ch13-functional-features/listing-13-06/src/main.rs @@ -10,14 +10,8 @@ fn generate_workout(intensity: u32, random_number: u32) { }; if intensity < 25 { - println!( - "Today, do {} pushups!", - expensive_closure(intensity) - ); - println!( - "Next, do {} situps!", - expensive_closure(intensity) - ); + println!("Today, do {} pushups!", expensive_closure(intensity)); + println!("Next, do {} situps!", expensive_closure(intensity)); } else { if random_number == 3 { println!("Take a break today! Remember to stay hydrated!"); @@ -35,8 +29,5 @@ fn main() { let simulated_user_specified_value = 10; let simulated_random_number = 7; - generate_workout( - simulated_user_specified_value, - simulated_random_number - ); + generate_workout(simulated_user_specified_value, simulated_random_number); } diff --git a/listings/ch13-functional-features/listing-13-07/src/main.rs b/listings/ch13-functional-features/listing-13-07/src/main.rs index 70e3c438b..b3f4cc2c2 100644 --- a/listings/ch13-functional-features/listing-13-07/src/main.rs +++ b/listings/ch13-functional-features/listing-13-07/src/main.rs @@ -11,14 +11,8 @@ fn generate_workout(intensity: u32, random_number: u32) { // ANCHOR_END: here if intensity < 25 { - println!( - "Today, do {} pushups!", - expensive_closure(intensity) - ); - println!( - "Next, do {} situps!", - expensive_closure(intensity) - ); + println!("Today, do {} pushups!", expensive_closure(intensity)); + println!("Next, do {} situps!", expensive_closure(intensity)); } else { if random_number == 3 { println!("Take a break today! Remember to stay hydrated!"); @@ -35,8 +29,5 @@ fn main() { let simulated_user_specified_value = 10; let simulated_random_number = 7; - generate_workout( - simulated_user_specified_value, - simulated_random_number - ); + generate_workout(simulated_user_specified_value, simulated_random_number); } diff --git a/listings/ch13-functional-features/listing-13-09/src/main.rs b/listings/ch13-functional-features/listing-13-09/src/main.rs index 79be866c2..3fd4ed067 100644 --- a/listings/ch13-functional-features/listing-13-09/src/main.rs +++ b/listings/ch13-functional-features/listing-13-09/src/main.rs @@ -1,6 +1,7 @@ // ANCHOR: here struct Cacher - where T: Fn(u32) -> u32 +where + T: Fn(u32) -> u32, { calculation: T, value: Option, diff --git a/listings/ch13-functional-features/listing-13-10/src/main.rs b/listings/ch13-functional-features/listing-13-10/src/main.rs index 29f47ac1a..4d1034db9 100644 --- a/listings/ch13-functional-features/listing-13-10/src/main.rs +++ b/listings/ch13-functional-features/listing-13-10/src/main.rs @@ -1,5 +1,6 @@ struct Cacher - where T: Fn(u32) -> u32 +where + T: Fn(u32) -> u32, { calculation: T, value: Option, @@ -7,7 +8,8 @@ struct Cacher // ANCHOR: here impl Cacher - where T: Fn(u32) -> u32 +where + T: Fn(u32) -> u32, { fn new(calculation: T) -> Cacher { Cacher { @@ -23,7 +25,7 @@ impl Cacher let v = (self.calculation)(arg); self.value = Some(v); v - }, + } } } } diff --git a/listings/ch13-functional-features/listing-13-11/src/main.rs b/listings/ch13-functional-features/listing-13-11/src/main.rs index 45cf2fbec..9f378b793 100644 --- a/listings/ch13-functional-features/listing-13-11/src/main.rs +++ b/listings/ch13-functional-features/listing-13-11/src/main.rs @@ -2,14 +2,16 @@ use std::thread; use std::time::Duration; struct Cacher - where T: Fn(u32) -> u32 +where + T: Fn(u32) -> u32, { calculation: T, value: Option, } impl Cacher - where T: Fn(u32) -> u32 +where + T: Fn(u32) -> u32, { fn new(calculation: T) -> Cacher { Cacher { @@ -25,7 +27,7 @@ impl Cacher let v = (self.calculation)(arg); self.value = Some(v); v - }, + } } } } @@ -39,14 +41,8 @@ fn generate_workout(intensity: u32, random_number: u32) { }); if intensity < 25 { - println!( - "Today, do {} pushups!", - expensive_result.value(intensity) - ); - println!( - "Next, do {} situps!", - expensive_result.value(intensity) - ); + println!("Today, do {} pushups!", expensive_result.value(intensity)); + println!("Next, do {} situps!", expensive_result.value(intensity)); } else { if random_number == 3 { println!("Take a break today! Remember to stay hydrated!"); @@ -64,8 +60,5 @@ fn main() { let simulated_user_specified_value = 10; let simulated_random_number = 7; - generate_workout( - simulated_user_specified_value, - simulated_random_number - ); + generate_workout(simulated_user_specified_value, simulated_random_number); } diff --git a/listings/ch13-functional-features/listing-13-19/src/lib.rs b/listings/ch13-functional-features/listing-13-19/src/lib.rs index a8b5cb555..c7ca364c2 100644 --- a/listings/ch13-functional-features/listing-13-19/src/lib.rs +++ b/listings/ch13-functional-features/listing-13-19/src/lib.rs @@ -5,9 +5,7 @@ struct Shoe { } fn shoes_in_my_size(shoes: Vec, shoe_size: u32) -> Vec { - shoes.into_iter() - .filter(|s| s.size == shoe_size) - .collect() + shoes.into_iter().filter(|s| s.size == shoe_size).collect() } #[cfg(test)] @@ -17,9 +15,18 @@ mod tests { #[test] fn filters_by_size() { let shoes = vec![ - Shoe { size: 10, style: String::from("sneaker") }, - Shoe { size: 13, style: String::from("sandal") }, - Shoe { size: 10, style: String::from("boot") }, + Shoe { + size: 10, + style: String::from("sneaker"), + }, + Shoe { + size: 13, + style: String::from("sandal"), + }, + Shoe { + size: 10, + style: String::from("boot"), + }, ]; let in_my_size = shoes_in_my_size(shoes, 10); @@ -27,8 +34,14 @@ mod tests { assert_eq!( in_my_size, vec![ - Shoe { size: 10, style: String::from("sneaker") }, - Shoe { size: 10, style: String::from("boot") }, + Shoe { + size: 10, + style: String::from("sneaker") + }, + Shoe { + size: 10, + style: String::from("boot") + }, ] ); } diff --git a/listings/ch13-functional-features/listing-13-23/src/lib.rs b/listings/ch13-functional-features/listing-13-23/src/lib.rs index ec8593bb9..941e29457 100644 --- a/listings/ch13-functional-features/listing-13-23/src/lib.rs +++ b/listings/ch13-functional-features/listing-13-23/src/lib.rs @@ -41,10 +41,11 @@ mod tests { // ANCHOR: here #[test] fn using_other_iterator_trait_methods() { - let sum: u32 = Counter::new().zip(Counter::new().skip(1)) - .map(|(a, b)| a * b) - .filter(|x| x % 3 == 0) - .sum(); + let sum: u32 = Counter::new() + .zip(Counter::new().skip(1)) + .map(|(a, b)| a * b) + .filter(|x| x % 3 == 0) + .sum(); assert_eq!(18, sum); } // ANCHOR_END: here diff --git a/listings/ch13-functional-features/listing-13-25/src/lib.rs b/listings/ch13-functional-features/listing-13-25/src/lib.rs index 10792052e..fe1dccf4e 100644 --- a/listings/ch13-functional-features/listing-13-25/src/lib.rs +++ b/listings/ch13-functional-features/listing-13-25/src/lib.rs @@ -19,7 +19,11 @@ impl Config { let case_sensitive = env::var("CASE_INSENSITIVE").is_err(); - Ok(Config { query, filename, case_sensitive }) + Ok(Config { + query, + filename, + case_sensitive, + }) } } @@ -51,7 +55,10 @@ pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { results } -pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { +pub fn search_case_insensitive<'a>( + query: &str, + contents: &'a str, +) -> Vec<&'a str> { let query = query.to_lowercase(); let mut results = Vec::new(); @@ -77,10 +84,7 @@ safe, fast, productive. Pick three. Duct tape."; - assert_eq!( - vec!["safe, fast, productive."], - search(query, contents) - ); + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); } #[test] diff --git a/listings/ch13-functional-features/listing-13-26/src/lib.rs b/listings/ch13-functional-features/listing-13-26/src/lib.rs index e980f44ef..44f0c7ee2 100644 --- a/listings/ch13-functional-features/listing-13-26/src/lib.rs +++ b/listings/ch13-functional-features/listing-13-26/src/lib.rs @@ -22,7 +22,11 @@ impl Config { let case_sensitive = env::var("CASE_INSENSITIVE").is_err(); - Ok(Config { query, filename, case_sensitive }) + Ok(Config { + query, + filename, + case_sensitive, + }) } } @@ -54,7 +58,10 @@ pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { results } -pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { +pub fn search_case_insensitive<'a>( + query: &str, + contents: &'a str, +) -> Vec<&'a str> { let query = query.to_lowercase(); let mut results = Vec::new(); @@ -80,10 +87,7 @@ safe, fast, productive. Pick three. Duct tape."; - assert_eq!( - vec!["safe, fast, productive."], - search(query, contents) - ); + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); } #[test] diff --git a/listings/ch13-functional-features/listing-13-27/src/lib.rs b/listings/ch13-functional-features/listing-13-27/src/lib.rs index b56edf515..a4c58d9bd 100644 --- a/listings/ch13-functional-features/listing-13-27/src/lib.rs +++ b/listings/ch13-functional-features/listing-13-27/src/lib.rs @@ -25,7 +25,11 @@ impl Config { let case_sensitive = env::var("CASE_INSENSITIVE").is_err(); - Ok(Config { query, filename, case_sensitive }) + Ok(Config { + query, + filename, + case_sensitive, + }) } } // ANCHOR_END: here @@ -58,7 +62,10 @@ pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { results } -pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { +pub fn search_case_insensitive<'a>( + query: &str, + contents: &'a str, +) -> Vec<&'a str> { let query = query.to_lowercase(); let mut results = Vec::new(); @@ -84,10 +91,7 @@ safe, fast, productive. Pick three. Duct tape."; - assert_eq!( - vec!["safe, fast, productive."], - search(query, contents) - ); + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); } #[test] diff --git a/listings/ch13-functional-features/listing-13-29/src/lib.rs b/listings/ch13-functional-features/listing-13-29/src/lib.rs index 555d797f9..bc8a77ef6 100644 --- a/listings/ch13-functional-features/listing-13-29/src/lib.rs +++ b/listings/ch13-functional-features/listing-13-29/src/lib.rs @@ -24,7 +24,11 @@ impl Config { let case_sensitive = env::var("CASE_INSENSITIVE").is_err(); - Ok(Config { query, filename, case_sensitive }) + Ok(Config { + query, + filename, + case_sensitive, + }) } } @@ -46,13 +50,17 @@ pub fn run(config: Config) -> Result<(), Box> { // ANCHOR: here pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { - contents.lines() + contents + .lines() .filter(|line| line.contains(query)) .collect() } // ANCHOR_END: here -pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { +pub fn search_case_insensitive<'a>( + query: &str, + contents: &'a str, +) -> Vec<&'a str> { let query = query.to_lowercase(); let mut results = Vec::new(); @@ -78,10 +86,7 @@ safe, fast, productive. Pick three. Duct tape."; - assert_eq!( - vec!["safe, fast, productive."], - search(query, contents) - ); + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); } #[test] diff --git a/listings/ch13-functional-features/no-listing-01-failing-cacher-test/output.txt b/listings/ch13-functional-features/no-listing-01-failing-cacher-test/output.txt index 096203fcd..5cbd97534 100644 --- a/listings/ch13-functional-features/no-listing-01-failing-cacher-test/output.txt +++ b/listings/ch13-functional-features/no-listing-01-failing-cacher-test/output.txt @@ -11,7 +11,7 @@ failures: ---- tests::call_with_different_values stdout ---- thread 'main' panicked at 'assertion failed: `(left == right)` left: `1`, - right: `2`', src/lib.rs:41:9 + right: `2`', src/lib.rs:43:9 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace. diff --git a/listings/ch13-functional-features/no-listing-01-failing-cacher-test/src/lib.rs b/listings/ch13-functional-features/no-listing-01-failing-cacher-test/src/lib.rs index 4ec6d9bf8..e7d677d0e 100644 --- a/listings/ch13-functional-features/no-listing-01-failing-cacher-test/src/lib.rs +++ b/listings/ch13-functional-features/no-listing-01-failing-cacher-test/src/lib.rs @@ -1,12 +1,14 @@ struct Cacher - where T: Fn(u32) -> u32 +where + T: Fn(u32) -> u32, { calculation: T, value: Option, } impl Cacher - where T: Fn(u32) -> u32 +where + T: Fn(u32) -> u32, { fn new(calculation: T) -> Cacher { Cacher { @@ -22,7 +24,7 @@ impl Cacher let v = (self.calculation)(arg); self.value = Some(v); v - }, + } } } } diff --git a/listings/ch13-functional-features/no-listing-02-functions-cant-capture/output.txt b/listings/ch13-functional-features/no-listing-02-functions-cant-capture/output.txt index 27886a958..527c4ec89 100644 --- a/listings/ch13-functional-features/no-listing-02-functions-cant-capture/output.txt +++ b/listings/ch13-functional-features/no-listing-02-functions-cant-capture/output.txt @@ -1,10 +1,10 @@ $ cargo run Compiling equal-to-x v0.1.0 (file:///projects/equal-to-x) error[E0434]: can't capture dynamic environment in a fn item - --> src/main.rs:4:42 + --> src/main.rs:5:14 | -4 | fn equal_to_x(z: i32) -> bool { z == x } - | ^ +5 | z == x + | ^ | = help: use the `|| { ... }` closure form instead diff --git a/listings/ch13-functional-features/no-listing-02-functions-cant-capture/src/main.rs b/listings/ch13-functional-features/no-listing-02-functions-cant-capture/src/main.rs index 032ecc889..1b5d2b930 100644 --- a/listings/ch13-functional-features/no-listing-02-functions-cant-capture/src/main.rs +++ b/listings/ch13-functional-features/no-listing-02-functions-cant-capture/src/main.rs @@ -1,7 +1,9 @@ fn main() { let x = 4; - fn equal_to_x(z: i32) -> bool { z == x } + fn equal_to_x(z: i32) -> bool { + z == x + } let y = 4; diff --git a/listings/ch14-more-about-cargo/listing-14-06/src/main.rs b/listings/ch14-more-about-cargo/listing-14-06/src/main.rs index 4b6e112af..51f3b761d 100644 --- a/listings/ch14-more-about-cargo/listing-14-06/src/main.rs +++ b/listings/ch14-more-about-cargo/listing-14-06/src/main.rs @@ -1,6 +1,6 @@ // ANCHOR: here -use art::PrimaryColor; use art::mix; +use art::PrimaryColor; fn main() { // --snip-- diff --git a/listings/ch14-more-about-cargo/listing-14-07/add/adder/src/main.rs b/listings/ch14-more-about-cargo/listing-14-07/add/adder/src/main.rs index cec9a65c3..7deb7962f 100644 --- a/listings/ch14-more-about-cargo/listing-14-07/add/adder/src/main.rs +++ b/listings/ch14-more-about-cargo/listing-14-07/add/adder/src/main.rs @@ -2,5 +2,9 @@ use add_one; fn main() { let num = 10; - println!("Hello, world! {} plus one is {}!", num, add_one::add_one(num)); + println!( + "Hello, world! {} plus one is {}!", + num, + add_one::add_one(num) + ); } diff --git a/listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/adder/src/main.rs b/listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/adder/src/main.rs index cec9a65c3..7deb7962f 100644 --- a/listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/adder/src/main.rs +++ b/listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/adder/src/main.rs @@ -2,5 +2,9 @@ use add_one; fn main() { let num = 10; - println!("Hello, world! {} plus one is {}!", num, add_one::add_one(num)); + println!( + "Hello, world! {} plus one is {}!", + num, + add_one::add_one(num) + ); } diff --git a/listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/adder/src/main.rs b/listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/adder/src/main.rs index cec9a65c3..7deb7962f 100644 --- a/listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/adder/src/main.rs +++ b/listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/adder/src/main.rs @@ -2,5 +2,9 @@ use add_one; fn main() { let num = 10; - println!("Hello, world! {} plus one is {}!", num, add_one::add_one(num)); + println!( + "Hello, world! {} plus one is {}!", + num, + add_one::add_one(num) + ); } diff --git a/listings/ch14-more-about-cargo/output-only-03-use-rand/add/adder/src/main.rs b/listings/ch14-more-about-cargo/output-only-03-use-rand/add/adder/src/main.rs index d08f1e596..eb4050dc3 100644 --- a/listings/ch14-more-about-cargo/output-only-03-use-rand/add/adder/src/main.rs +++ b/listings/ch14-more-about-cargo/output-only-03-use-rand/add/adder/src/main.rs @@ -1,7 +1,11 @@ -use rand; use add_one; +use rand; fn main() { let num = 10; - println!("Hello, world! {} plus one is {}!", num, add_one::add_one(num)); + println!( + "Hello, world! {} plus one is {}!", + num, + add_one::add_one(num) + ); } diff --git a/listings/ch15-smart-pointers/listing-15-05/src/main.rs b/listings/ch15-smart-pointers/listing-15-05/src/main.rs index a3f6c19fc..22f7d8338 100644 --- a/listings/ch15-smart-pointers/listing-15-05/src/main.rs +++ b/listings/ch15-smart-pointers/listing-15-05/src/main.rs @@ -6,8 +6,5 @@ enum List { use crate::List::{Cons, Nil}; fn main() { - let list = Cons(1, - Box::new(Cons(2, - Box::new(Cons(3, - Box::new(Nil)))))); + let list = Cons(1, Box::new(Cons(2, Box::new(Cons(3, Box::new(Nil)))))); } diff --git a/listings/ch15-smart-pointers/listing-15-08/src/main.rs b/listings/ch15-smart-pointers/listing-15-08/src/main.rs index 8cbf312d9..f48594673 100644 --- a/listings/ch15-smart-pointers/listing-15-08/src/main.rs +++ b/listings/ch15-smart-pointers/listing-15-08/src/main.rs @@ -8,5 +8,4 @@ impl MyBox { } // ANCHOR_END: here -fn main() { -} +fn main() {} diff --git a/listings/ch15-smart-pointers/listing-15-11/src/main.rs b/listings/ch15-smart-pointers/listing-15-11/src/main.rs index 76ffd531f..b73ad89c2 100644 --- a/listings/ch15-smart-pointers/listing-15-11/src/main.rs +++ b/listings/ch15-smart-pointers/listing-15-11/src/main.rs @@ -4,5 +4,4 @@ fn hello(name: &str) { } // ANCHOR_END: here -fn main() { -} +fn main() {} diff --git a/listings/ch15-smart-pointers/listing-15-14/src/main.rs b/listings/ch15-smart-pointers/listing-15-14/src/main.rs index c5b1bb0cd..231612ae6 100644 --- a/listings/ch15-smart-pointers/listing-15-14/src/main.rs +++ b/listings/ch15-smart-pointers/listing-15-14/src/main.rs @@ -9,7 +9,11 @@ impl Drop for CustomSmartPointer { } fn main() { - let c = CustomSmartPointer { data: String::from("my stuff") }; - let d = CustomSmartPointer { data: String::from("other stuff") }; + let c = CustomSmartPointer { + data: String::from("my stuff"), + }; + let d = CustomSmartPointer { + data: String::from("other stuff"), + }; println!("CustomSmartPointers created."); } diff --git a/listings/ch15-smart-pointers/listing-15-15/output.txt b/listings/ch15-smart-pointers/listing-15-15/output.txt index 30dd9958d..2cc4dd48d 100644 --- a/listings/ch15-smart-pointers/listing-15-15/output.txt +++ b/listings/ch15-smart-pointers/listing-15-15/output.txt @@ -1,9 +1,9 @@ $ cargo run Compiling drop-example v0.1.0 (file:///projects/drop-example) error[E0040]: explicit use of destructor method - --> src/main.rs:14:7 + --> src/main.rs:16:7 | -14 | c.drop(); +16 | c.drop(); | ^^^^ explicit destructor calls not allowed error: aborting due to previous error diff --git a/listings/ch15-smart-pointers/listing-15-15/src/main.rs b/listings/ch15-smart-pointers/listing-15-15/src/main.rs index 212d36114..ff3b391a9 100644 --- a/listings/ch15-smart-pointers/listing-15-15/src/main.rs +++ b/listings/ch15-smart-pointers/listing-15-15/src/main.rs @@ -10,7 +10,9 @@ impl Drop for CustomSmartPointer { // ANCHOR: here fn main() { - let c = CustomSmartPointer { data: String::from("some data") }; + let c = CustomSmartPointer { + data: String::from("some data"), + }; println!("CustomSmartPointer created."); c.drop(); println!("CustomSmartPointer dropped before the end of main."); diff --git a/listings/ch15-smart-pointers/listing-15-16/src/main.rs b/listings/ch15-smart-pointers/listing-15-16/src/main.rs index c31fd3aa0..f11715c45 100644 --- a/listings/ch15-smart-pointers/listing-15-16/src/main.rs +++ b/listings/ch15-smart-pointers/listing-15-16/src/main.rs @@ -10,7 +10,9 @@ impl Drop for CustomSmartPointer { // ANCHOR: here fn main() { - let c = CustomSmartPointer { data: String::from("some data") }; + let c = CustomSmartPointer { + data: String::from("some data"), + }; println!("CustomSmartPointer created."); drop(c); println!("CustomSmartPointer dropped before the end of main."); diff --git a/listings/ch15-smart-pointers/listing-15-17/output.txt b/listings/ch15-smart-pointers/listing-15-17/output.txt index 7b3cda4b2..0c7365b58 100644 --- a/listings/ch15-smart-pointers/listing-15-17/output.txt +++ b/listings/ch15-smart-pointers/listing-15-17/output.txt @@ -1,14 +1,13 @@ $ cargo run Compiling cons-list v0.1.0 (file:///projects/cons-list) error[E0382]: use of moved value: `a` - --> src/main.rs:13:30 + --> src/main.rs:11:30 | -9 | let a = Cons(5, +9 | let a = Cons(5, Box::new(Cons(10, Box::new(Nil)))); | - move occurs because `a` has type `List`, which does not implement the `Copy` trait -... -12 | let b = Cons(3, Box::new(a)); +10 | let b = Cons(3, Box::new(a)); | - value moved here -13 | let c = Cons(4, Box::new(a)); +11 | let c = Cons(4, Box::new(a)); | ^ value used here after move error: aborting due to previous error diff --git a/listings/ch15-smart-pointers/listing-15-17/src/main.rs b/listings/ch15-smart-pointers/listing-15-17/src/main.rs index a99981138..47c33e4c4 100644 --- a/listings/ch15-smart-pointers/listing-15-17/src/main.rs +++ b/listings/ch15-smart-pointers/listing-15-17/src/main.rs @@ -6,9 +6,7 @@ enum List { use crate::List::{Cons, Nil}; fn main() { - let a = Cons(5, - Box::new(Cons(10, - Box::new(Nil)))); + let a = Cons(5, Box::new(Cons(10, Box::new(Nil)))); let b = Cons(3, Box::new(a)); let c = Cons(4, Box::new(a)); } diff --git a/listings/ch15-smart-pointers/listing-15-20/src/lib.rs b/listings/ch15-smart-pointers/listing-15-20/src/lib.rs index f229e8a79..d3a900373 100644 --- a/listings/ch15-smart-pointers/listing-15-20/src/lib.rs +++ b/listings/ch15-smart-pointers/listing-15-20/src/lib.rs @@ -9,7 +9,9 @@ pub struct LimitTracker<'a, T: Messenger> { } impl<'a, T> LimitTracker<'a, T> - where T: Messenger { +where + T: Messenger, +{ pub fn new(messenger: &T, max: usize) -> LimitTracker { LimitTracker { messenger, @@ -26,9 +28,11 @@ impl<'a, T> LimitTracker<'a, T> if percentage_of_max >= 1.0 { self.messenger.send("Error: You are over your quota!"); } else if percentage_of_max >= 0.9 { - self.messenger.send("Urgent warning: You've used up over 90% of your quota!"); + self.messenger + .send("Urgent warning: You've used up over 90% of your quota!"); } else if percentage_of_max >= 0.75 { - self.messenger.send("Warning: You've used up over 75% of your quota!"); + self.messenger + .send("Warning: You've used up over 75% of your quota!"); } } } diff --git a/listings/ch15-smart-pointers/listing-15-21/output.txt b/listings/ch15-smart-pointers/listing-15-21/output.txt index 807191910..615cd11d6 100644 --- a/listings/ch15-smart-pointers/listing-15-21/output.txt +++ b/listings/ch15-smart-pointers/listing-15-21/output.txt @@ -1,11 +1,11 @@ $ cargo test Compiling limit-tracker v0.1.0 (file:///projects/limit-tracker) error[E0596]: cannot borrow `self.sent_messages` as mutable, as it is behind a `&` reference - --> src/lib.rs:52:13 + --> src/lib.rs:58:13 | -51 | fn send(&self, message: &str) { +57 | fn send(&self, message: &str) { | ----- help: consider changing this to be a mutable reference: `&mut self` -52 | self.sent_messages.push(String::from(message)); +58 | self.sent_messages.push(String::from(message)); | ^^^^^^^^^^^^^^^^^^ `self` is a `&` reference, so the data it refers to cannot be borrowed as mutable error: aborting due to previous error diff --git a/listings/ch15-smart-pointers/listing-15-21/src/lib.rs b/listings/ch15-smart-pointers/listing-15-21/src/lib.rs index 7f84c7be2..9e403e3ca 100644 --- a/listings/ch15-smart-pointers/listing-15-21/src/lib.rs +++ b/listings/ch15-smart-pointers/listing-15-21/src/lib.rs @@ -9,7 +9,9 @@ pub struct LimitTracker<'a, T: Messenger> { } impl<'a, T> LimitTracker<'a, T> - where T: Messenger { +where + T: Messenger, +{ pub fn new(messenger: &T, max: usize) -> LimitTracker { LimitTracker { messenger, @@ -26,9 +28,11 @@ impl<'a, T> LimitTracker<'a, T> if percentage_of_max >= 1.0 { self.messenger.send("Error: You are over your quota!"); } else if percentage_of_max >= 0.9 { - self.messenger.send("Urgent warning: You've used up over 90% of your quota!"); + self.messenger + .send("Urgent warning: You've used up over 90% of your quota!"); } else if percentage_of_max >= 0.75 { - self.messenger.send("Warning: You've used up over 75% of your quota!"); + self.messenger + .send("Warning: You've used up over 75% of your quota!"); } } } @@ -44,7 +48,9 @@ mod tests { impl MockMessenger { fn new() -> MockMessenger { - MockMessenger { sent_messages: vec![] } + MockMessenger { + sent_messages: vec![], + } } } diff --git a/listings/ch15-smart-pointers/listing-15-22/src/lib.rs b/listings/ch15-smart-pointers/listing-15-22/src/lib.rs index ac2298cf0..dc259f7fc 100644 --- a/listings/ch15-smart-pointers/listing-15-22/src/lib.rs +++ b/listings/ch15-smart-pointers/listing-15-22/src/lib.rs @@ -9,7 +9,9 @@ pub struct LimitTracker<'a, T: Messenger> { } impl<'a, T> LimitTracker<'a, T> - where T: Messenger { +where + T: Messenger, +{ pub fn new(messenger: &T, max: usize) -> LimitTracker { LimitTracker { messenger, @@ -26,9 +28,11 @@ impl<'a, T> LimitTracker<'a, T> if percentage_of_max >= 1.0 { self.messenger.send("Error: You are over your quota!"); } else if percentage_of_max >= 0.9 { - self.messenger.send("Urgent warning: You've used up over 90% of your quota!"); + self.messenger + .send("Urgent warning: You've used up over 90% of your quota!"); } else if percentage_of_max >= 0.75 { - self.messenger.send("Warning: You've used up over 75% of your quota!"); + self.messenger + .send("Warning: You've used up over 75% of your quota!"); } } } @@ -45,7 +49,9 @@ mod tests { impl MockMessenger { fn new() -> MockMessenger { - MockMessenger { sent_messages: RefCell::new(vec![]) } + MockMessenger { + sent_messages: RefCell::new(vec![]), + } } } diff --git a/listings/ch15-smart-pointers/listing-15-23/src/lib.rs b/listings/ch15-smart-pointers/listing-15-23/src/lib.rs index 64065e1b9..4e599ddc8 100644 --- a/listings/ch15-smart-pointers/listing-15-23/src/lib.rs +++ b/listings/ch15-smart-pointers/listing-15-23/src/lib.rs @@ -9,7 +9,9 @@ pub struct LimitTracker<'a, T: Messenger> { } impl<'a, T> LimitTracker<'a, T> - where T: Messenger { +where + T: Messenger, +{ pub fn new(messenger: &T, max: usize) -> LimitTracker { LimitTracker { messenger, @@ -26,9 +28,11 @@ impl<'a, T> LimitTracker<'a, T> if percentage_of_max >= 1.0 { self.messenger.send("Error: You are over your quota!"); } else if percentage_of_max >= 0.9 { - self.messenger.send("Urgent warning: You've used up over 90% of your quota!"); + self.messenger + .send("Urgent warning: You've used up over 90% of your quota!"); } else if percentage_of_max >= 0.75 { - self.messenger.send("Warning: You've used up over 75% of your quota!"); + self.messenger + .send("Warning: You've used up over 75% of your quota!"); } } } @@ -44,7 +48,9 @@ mod tests { impl MockMessenger { fn new() -> MockMessenger { - MockMessenger { sent_messages: RefCell::new(vec![]) } + MockMessenger { + sent_messages: RefCell::new(vec![]), + } } } diff --git a/listings/ch15-smart-pointers/listing-15-24/src/main.rs b/listings/ch15-smart-pointers/listing-15-24/src/main.rs index cd3717fc4..ac271fa21 100644 --- a/listings/ch15-smart-pointers/listing-15-24/src/main.rs +++ b/listings/ch15-smart-pointers/listing-15-24/src/main.rs @@ -5,8 +5,8 @@ enum List { } use crate::List::{Cons, Nil}; -use std::rc::Rc; use std::cell::RefCell; +use std::rc::Rc; fn main() { let value = Rc::new(RefCell::new(5)); diff --git a/listings/ch15-smart-pointers/listing-15-25/src/main.rs b/listings/ch15-smart-pointers/listing-15-25/src/main.rs index 3829d0bdd..f36c7fd06 100644 --- a/listings/ch15-smart-pointers/listing-15-25/src/main.rs +++ b/listings/ch15-smart-pointers/listing-15-25/src/main.rs @@ -1,6 +1,6 @@ -use std::rc::Rc; -use std::cell::RefCell; use crate::List::{Cons, Nil}; +use std::cell::RefCell; +use std::rc::Rc; #[derive(Debug)] enum List { diff --git a/listings/ch15-smart-pointers/listing-15-26/src/main.rs b/listings/ch15-smart-pointers/listing-15-26/src/main.rs index 128c4626d..08963aaa5 100644 --- a/listings/ch15-smart-pointers/listing-15-26/src/main.rs +++ b/listings/ch15-smart-pointers/listing-15-26/src/main.rs @@ -1,6 +1,6 @@ -use std::rc::Rc; -use std::cell::RefCell; use crate::List::{Cons, Nil}; +use std::cell::RefCell; +use std::rc::Rc; #[derive(Debug)] enum List { diff --git a/listings/ch15-smart-pointers/listing-15-27/src/main.rs b/listings/ch15-smart-pointers/listing-15-27/src/main.rs index 8bd71f921..335d154dd 100644 --- a/listings/ch15-smart-pointers/listing-15-27/src/main.rs +++ b/listings/ch15-smart-pointers/listing-15-27/src/main.rs @@ -1,6 +1,6 @@ // ANCHOR: here -use std::rc::Rc; use std::cell::RefCell; +use std::rc::Rc; #[derive(Debug)] struct Node { diff --git a/listings/ch15-smart-pointers/listing-15-28/src/main.rs b/listings/ch15-smart-pointers/listing-15-28/src/main.rs index 3181f591f..fabd1cbce 100644 --- a/listings/ch15-smart-pointers/listing-15-28/src/main.rs +++ b/listings/ch15-smart-pointers/listing-15-28/src/main.rs @@ -1,6 +1,6 @@ // ANCHOR: here -use std::rc::{Rc, Weak}; use std::cell::RefCell; +use std::rc::{Rc, Weak}; #[derive(Debug)] struct Node { diff --git a/listings/ch15-smart-pointers/listing-15-29/src/main.rs b/listings/ch15-smart-pointers/listing-15-29/src/main.rs index b87447d5c..ea13df0eb 100644 --- a/listings/ch15-smart-pointers/listing-15-29/src/main.rs +++ b/listings/ch15-smart-pointers/listing-15-29/src/main.rs @@ -1,5 +1,5 @@ -use std::rc::{Rc, Weak}; use std::cell::RefCell; +use std::rc::{Rc, Weak}; #[derive(Debug)] struct Node { diff --git a/listings/ch16-fearless-concurrency/listing-16-07/src/main.rs b/listings/ch16-fearless-concurrency/listing-16-07/src/main.rs index 40da80144..7859b64da 100644 --- a/listings/ch16-fearless-concurrency/listing-16-07/src/main.rs +++ b/listings/ch16-fearless-concurrency/listing-16-07/src/main.rs @@ -1,5 +1,5 @@ -use std::thread; use std::sync::mpsc; +use std::thread; fn main() { let (tx, rx) = mpsc::channel(); diff --git a/listings/ch16-fearless-concurrency/listing-16-08/src/main.rs b/listings/ch16-fearless-concurrency/listing-16-08/src/main.rs index bd950652b..fbba9167d 100644 --- a/listings/ch16-fearless-concurrency/listing-16-08/src/main.rs +++ b/listings/ch16-fearless-concurrency/listing-16-08/src/main.rs @@ -1,5 +1,5 @@ -use std::thread; use std::sync::mpsc; +use std::thread; fn main() { let (tx, rx) = mpsc::channel(); diff --git a/listings/ch16-fearless-concurrency/listing-16-09/src/main.rs b/listings/ch16-fearless-concurrency/listing-16-09/src/main.rs index 1d87a5efb..98a8129ab 100644 --- a/listings/ch16-fearless-concurrency/listing-16-09/src/main.rs +++ b/listings/ch16-fearless-concurrency/listing-16-09/src/main.rs @@ -1,5 +1,5 @@ -use std::thread; use std::sync::mpsc; +use std::thread; fn main() { let (tx, rx) = mpsc::channel(); diff --git a/listings/ch16-fearless-concurrency/listing-16-10/src/main.rs b/listings/ch16-fearless-concurrency/listing-16-10/src/main.rs index fb0b042f0..82b220de4 100644 --- a/listings/ch16-fearless-concurrency/listing-16-10/src/main.rs +++ b/listings/ch16-fearless-concurrency/listing-16-10/src/main.rs @@ -1,5 +1,5 @@ -use std::thread; use std::sync::mpsc; +use std::thread; use std::time::Duration; fn main() { diff --git a/listings/ch16-fearless-concurrency/listing-16-11/src/main.rs b/listings/ch16-fearless-concurrency/listing-16-11/src/main.rs index be335c471..f0acf4c2d 100644 --- a/listings/ch16-fearless-concurrency/listing-16-11/src/main.rs +++ b/listings/ch16-fearless-concurrency/listing-16-11/src/main.rs @@ -1,5 +1,5 @@ -use std::thread; use std::sync::mpsc; +use std::thread; use std::time::Duration; fn main() { diff --git a/listings/ch16-fearless-concurrency/listing-16-15/src/main.rs b/listings/ch16-fearless-concurrency/listing-16-15/src/main.rs index 589c6b47f..30247dd52 100644 --- a/listings/ch16-fearless-concurrency/listing-16-15/src/main.rs +++ b/listings/ch16-fearless-concurrency/listing-16-15/src/main.rs @@ -1,4 +1,4 @@ -use std::sync::{Mutex, Arc}; +use std::sync::{Arc, Mutex}; use std::thread; fn main() { diff --git a/listings/ch17-oop/listing-17-02/src/lib.rs b/listings/ch17-oop/listing-17-02/src/lib.rs index 578123247..bb407ec5f 100644 --- a/listings/ch17-oop/listing-17-02/src/lib.rs +++ b/listings/ch17-oop/listing-17-02/src/lib.rs @@ -16,7 +16,7 @@ impl AveragedCollection { Some(value) => { self.update_average(); Some(value) - }, + } None => None, } } diff --git a/listings/ch17-oop/listing-17-06/src/lib.rs b/listings/ch17-oop/listing-17-06/src/lib.rs index 1abe6ea3a..63a8907d3 100644 --- a/listings/ch17-oop/listing-17-06/src/lib.rs +++ b/listings/ch17-oop/listing-17-06/src/lib.rs @@ -8,7 +8,9 @@ pub struct Screen { } impl Screen - where T: Draw { +where + T: Draw, +{ pub fn run(&self) { for component in self.components.iter() { component.draw(); diff --git a/listings/ch17-oop/listing-17-09/src/main.rs b/listings/ch17-oop/listing-17-09/src/main.rs index ff06f65b1..4eb13f6b7 100644 --- a/listings/ch17-oop/listing-17-09/src/main.rs +++ b/listings/ch17-oop/listing-17-09/src/main.rs @@ -13,7 +13,7 @@ impl Draw for SelectBox { } // ANCHOR: here -use gui::{Screen, Button}; +use gui::{Button, Screen}; fn main() { let screen = Screen { @@ -24,7 +24,7 @@ fn main() { options: vec![ String::from("Yes"), String::from("Maybe"), - String::from("No") + String::from("No"), ], }), Box::new(Button { diff --git a/listings/ch17-oop/listing-17-10/output.txt b/listings/ch17-oop/listing-17-10/output.txt index 5faf3ef4f..210fd1838 100644 --- a/listings/ch17-oop/listing-17-10/output.txt +++ b/listings/ch17-oop/listing-17-10/output.txt @@ -1,10 +1,10 @@ $ cargo run Compiling gui v0.1.0 (file:///projects/gui) error[E0277]: the trait bound `std::string::String: gui::Draw` is not satisfied - --> src/main.rs:6:13 + --> src/main.rs:5:26 | -6 | Box::new(String::from("Hi")), - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `gui::Draw` is not implemented for `std::string::String` +5 | components: vec![Box::new(String::from("Hi"))], + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `gui::Draw` is not implemented for `std::string::String` | = note: required for the cast to the object type `dyn gui::Draw` diff --git a/listings/ch17-oop/listing-17-10/src/main.rs b/listings/ch17-oop/listing-17-10/src/main.rs index a8485dee7..2ede87ab7 100644 --- a/listings/ch17-oop/listing-17-10/src/main.rs +++ b/listings/ch17-oop/listing-17-10/src/main.rs @@ -2,9 +2,7 @@ use gui::Screen; fn main() { let screen = Screen { - components: vec![ - Box::new(String::from("Hi")), - ], + components: vec![Box::new(String::from("Hi"))], }; screen.run(); diff --git a/listings/ch17-oop/listing-17-17/src/lib.rs b/listings/ch17-oop/listing-17-17/src/lib.rs index e514cea89..0beee7b8d 100644 --- a/listings/ch17-oop/listing-17-17/src/lib.rs +++ b/listings/ch17-oop/listing-17-17/src/lib.rs @@ -79,4 +79,4 @@ impl State for Published { fn approve(self: Box) -> Box { self } -} \ No newline at end of file +} diff --git a/listings/ch18-patterns-and-matching/listing-18-15/src/main.rs b/listings/ch18-patterns-and-matching/listing-18-15/src/main.rs index 259aaca85..9b8dac193 100644 --- a/listings/ch18-patterns-and-matching/listing-18-15/src/main.rs +++ b/listings/ch18-patterns-and-matching/listing-18-15/src/main.rs @@ -15,18 +15,13 @@ fn main() { Message::Move { x, y } => { println!( "Move in the x direction {} and in the y direction {}", - x, - y + x, y ); } Message::Write(text) => println!("Text message: {}", text), - Message::ChangeColor(r, g, b) => { - println!( - "Change the color to red {}, green {}, and blue {}", - r, - g, - b - ) - } + Message::ChangeColor(r, g, b) => println!( + "Change the color to red {}, green {}, and blue {}", + r, g, b + ), } } diff --git a/listings/ch18-patterns-and-matching/listing-18-16/src/main.rs b/listings/ch18-patterns-and-matching/listing-18-16/src/main.rs index 61757a367..ed6a20bf4 100644 --- a/listings/ch18-patterns-and-matching/listing-18-16/src/main.rs +++ b/listings/ch18-patterns-and-matching/listing-18-16/src/main.rs @@ -1,6 +1,6 @@ enum Color { - Rgb(i32, i32, i32), - Hsv(i32, i32, i32), + Rgb(i32, i32, i32), + Hsv(i32, i32, i32), } enum Message { @@ -14,22 +14,14 @@ fn main() { let msg = Message::ChangeColor(Color::Hsv(0, 160, 255)); match msg { - Message::ChangeColor(Color::Rgb(r, g, b)) => { - println!( - "Change the color to red {}, green {}, and blue {}", - r, - g, - b - ) - } - Message::ChangeColor(Color::Hsv(h, s, v)) => { - println!( - "Change the color to hue {}, saturation {}, and value {}", - h, - s, - v - ) - } - _ => () + Message::ChangeColor(Color::Rgb(r, g, b)) => println!( + "Change the color to red {}, green {}, and blue {}", + r, g, b + ), + Message::ChangeColor(Color::Hsv(h, s, v)) => println!( + "Change the color to hue {}, saturation {}, and value {}", + h, s, v + ), + _ => (), } } diff --git a/listings/ch18-patterns-and-matching/listing-18-19/src/main.rs b/listings/ch18-patterns-and-matching/listing-18-19/src/main.rs index 4bc1ff54f..59b48c94d 100644 --- a/listings/ch18-patterns-and-matching/listing-18-19/src/main.rs +++ b/listings/ch18-patterns-and-matching/listing-18-19/src/main.rs @@ -5,7 +5,7 @@ fn main() { match numbers { (first, _, third, _, fifth) => { println!("Some numbers: {}, {}, {}", first, third, fifth) - }, + } } // ANCHOR_END: here } diff --git a/listings/ch18-patterns-and-matching/listing-18-24/src/main.rs b/listings/ch18-patterns-and-matching/listing-18-24/src/main.rs index 8490bdd56..f22dbe8ed 100644 --- a/listings/ch18-patterns-and-matching/listing-18-24/src/main.rs +++ b/listings/ch18-patterns-and-matching/listing-18-24/src/main.rs @@ -4,6 +4,6 @@ fn main() { match numbers { (first, .., last) => { println!("Some numbers: {}, {}", first, last); - }, + } } } diff --git a/listings/ch18-patterns-and-matching/listing-18-29/src/main.rs b/listings/ch18-patterns-and-matching/listing-18-29/src/main.rs index f5b873996..3514deb63 100644 --- a/listings/ch18-patterns-and-matching/listing-18-29/src/main.rs +++ b/listings/ch18-patterns-and-matching/listing-18-29/src/main.rs @@ -7,15 +7,13 @@ fn main() { let msg = Message::Hello { id: 5 }; match msg { - Message::Hello { id: id_variable @ 3..=7 } => { - println!("Found an id in range: {}", id_variable) - }, + Message::Hello { + id: id_variable @ 3..=7, + } => println!("Found an id in range: {}", id_variable), Message::Hello { id: 10..=12 } => { println!("Found an id in another range") - }, - Message::Hello { id } => { - println!("Found some other id: {}", id) - }, + } + Message::Hello { id } => println!("Found some other id: {}", id), } // ANCHOR_END: here } diff --git a/listings/ch18-patterns-and-matching/no-listing-05-destructuring-structs-and-tuples/src/main.rs b/listings/ch18-patterns-and-matching/no-listing-05-destructuring-structs-and-tuples/src/main.rs index 67f98ed4c..962d09349 100644 --- a/listings/ch18-patterns-and-matching/no-listing-05-destructuring-structs-and-tuples/src/main.rs +++ b/listings/ch18-patterns-and-matching/no-listing-05-destructuring-structs-and-tuples/src/main.rs @@ -5,6 +5,6 @@ fn main() { } // ANCHOR: here - let ((feet, inches), Point {x, y}) = ((3, 10), Point { x: 3, y: -10 }); + let ((feet, inches), Point { x, y }) = ((3, 10), Point { x: 3, y: -10 }); // ANCHOR_END: here } diff --git a/listings/ch19-advanced-features/listing-19-05/output.txt b/listings/ch19-advanced-features/listing-19-05/output.txt index 9486066e2..0283399c7 100644 --- a/listings/ch19-advanced-features/listing-19-05/output.txt +++ b/listings/ch19-advanced-features/listing-19-05/output.txt @@ -1,19 +1,17 @@ $ cargo run Compiling unsafe-example v0.1.0 (file:///projects/unsafe-example) error[E0499]: cannot borrow `*slice` as mutable more than once at a time - --> src/main.rs:7:11 + --> src/main.rs:6:30 | -1 | fn split_at_mut(slice: &mut [i32], mid: usize) -> (&mut [i32], &mut [i32]) { - | - let's call the lifetime of this reference `'1` +1 | fn split_at_mut(slice: &mut [i32], mid: usize) -> (&mut [i32], &mut [i32]) { + | - let's call the lifetime of this reference `'1` ... -6 | (&mut slice[..mid], - | - ----- first mutable borrow occurs here - | _____| - | | -7 | | &mut slice[mid..]) - | |___________^^^^^_______- returning this value requires that `*slice` is borrowed for `'1` - | | - | second mutable borrow occurs here +6 | (&mut slice[..mid], &mut slice[mid..]) + | -------------------------^^^^^-------- + | | | | + | | | second mutable borrow occurs here + | | first mutable borrow occurs here + | returning this value requires that `*slice` is borrowed for `'1` error: aborting due to previous error diff --git a/listings/ch19-advanced-features/listing-19-05/src/main.rs b/listings/ch19-advanced-features/listing-19-05/src/main.rs index 549fcfab2..c4b83effa 100644 --- a/listings/ch19-advanced-features/listing-19-05/src/main.rs +++ b/listings/ch19-advanced-features/listing-19-05/src/main.rs @@ -4,8 +4,7 @@ fn split_at_mut(slice: &mut [i32], mid: usize) -> (&mut [i32], &mut [i32]) { assert!(mid <= len); - (&mut slice[..mid], - &mut slice[mid..]) + (&mut slice[..mid], &mut slice[mid..]) } // ANCHOR_END: here diff --git a/listings/ch19-advanced-features/listing-19-06/src/main.rs b/listings/ch19-advanced-features/listing-19-06/src/main.rs index f53f0000a..2bc2e5c70 100644 --- a/listings/ch19-advanced-features/listing-19-06/src/main.rs +++ b/listings/ch19-advanced-features/listing-19-06/src/main.rs @@ -8,8 +8,10 @@ fn split_at_mut(slice: &mut [i32], mid: usize) -> (&mut [i32], &mut [i32]) { assert!(mid <= len); unsafe { - (slice::from_raw_parts_mut(ptr, mid), - slice::from_raw_parts_mut(ptr.offset(mid as isize), len - mid)) + ( + slice::from_raw_parts_mut(ptr, mid), + slice::from_raw_parts_mut(ptr.offset(mid as isize), len - mid), + ) } } // ANCHOR_END: here diff --git a/listings/ch19-advanced-features/listing-19-07/src/main.rs b/listings/ch19-advanced-features/listing-19-07/src/main.rs index c5586a287..0ab39ae1d 100644 --- a/listings/ch19-advanced-features/listing-19-07/src/main.rs +++ b/listings/ch19-advanced-features/listing-19-07/src/main.rs @@ -5,8 +5,6 @@ fn main() { let address = 0x01234usize; let r = address as *mut i32; - let slice: &[i32] = unsafe { - slice::from_raw_parts_mut(r, 10000) - }; + let slice: &[i32] = unsafe { slice::from_raw_parts_mut(r, 10000) }; // ANCHOR_END: here } diff --git a/listings/ch19-advanced-features/listing-19-14/src/main.rs b/listings/ch19-advanced-features/listing-19-14/src/main.rs index 89f357270..7dea568d9 100644 --- a/listings/ch19-advanced-features/listing-19-14/src/main.rs +++ b/listings/ch19-advanced-features/listing-19-14/src/main.rs @@ -18,6 +18,8 @@ impl Add for Point { } fn main() { - assert_eq!(Point { x: 1, y: 0 } + Point { x: 2, y: 3 }, - Point { x: 3, y: 3 }); + assert_eq!( + Point { x: 1, y: 0 } + Point { x: 2, y: 3 }, + Point { x: 3, y: 3 } + ); } diff --git a/listings/ch19-advanced-features/listing-19-24/src/main.rs b/listings/ch19-advanced-features/listing-19-24/src/main.rs index 20f26fce2..d604ae8d6 100644 --- a/listings/ch19-advanced-features/listing-19-24/src/main.rs +++ b/listings/ch19-advanced-features/listing-19-24/src/main.rs @@ -3,11 +3,11 @@ fn main() { let f: Box = Box::new(|| println!("hi")); fn takes_long_type(f: Box) { - // --snip-- + // --snip-- } fn returns_long_type() -> Box { - // --snip-- + // --snip-- // ANCHOR_END: here Box::new(|| ()) // ANCHOR: here diff --git a/listings/ch19-advanced-features/no-listing-05-write-trait/src/lib.rs b/listings/ch19-advanced-features/no-listing-05-write-trait/src/lib.rs index 00de3f0bf..8300dccee 100644 --- a/listings/ch19-advanced-features/no-listing-05-write-trait/src/lib.rs +++ b/listings/ch19-advanced-features/no-listing-05-write-trait/src/lib.rs @@ -1,5 +1,5 @@ -use std::io::Error; use std::fmt; +use std::io::Error; pub trait Write { fn write(&mut self, buf: &[u8]) -> Result; diff --git a/listings/ch19-advanced-features/no-listing-15-map-closure/src/main.rs b/listings/ch19-advanced-features/no-listing-15-map-closure/src/main.rs index 9372c0de4..b4fcf7eb8 100644 --- a/listings/ch19-advanced-features/no-listing-15-map-closure/src/main.rs +++ b/listings/ch19-advanced-features/no-listing-15-map-closure/src/main.rs @@ -1,9 +1,7 @@ fn main() { // ANCHOR: here let list_of_numbers = vec![1, 2, 3]; - let list_of_strings: Vec = list_of_numbers - .iter() - .map(|i| i.to_string()) - .collect(); + let list_of_strings: Vec = + list_of_numbers.iter().map(|i| i.to_string()).collect(); // ANCHOR_END: here } diff --git a/listings/ch19-advanced-features/no-listing-16-map-function/src/main.rs b/listings/ch19-advanced-features/no-listing-16-map-function/src/main.rs index 66d50f895..dff20fe71 100644 --- a/listings/ch19-advanced-features/no-listing-16-map-function/src/main.rs +++ b/listings/ch19-advanced-features/no-listing-16-map-function/src/main.rs @@ -1,9 +1,7 @@ fn main() { // ANCHOR: here let list_of_numbers = vec![1, 2, 3]; - let list_of_strings: Vec = list_of_numbers - .iter() - .map(ToString::to_string) - .collect(); + let list_of_strings: Vec = + list_of_numbers.iter().map(ToString::to_string).collect(); // ANCHOR_END: here } diff --git a/listings/ch19-advanced-features/no-listing-17-map-initializer/src/main.rs b/listings/ch19-advanced-features/no-listing-17-map-initializer/src/main.rs index a237e4247..60fb73005 100644 --- a/listings/ch19-advanced-features/no-listing-17-map-initializer/src/main.rs +++ b/listings/ch19-advanced-features/no-listing-17-map-initializer/src/main.rs @@ -5,9 +5,6 @@ fn main() { Stop, } - let list_of_statuses: Vec = - (0u32..20) - .map(Status::Value) - .collect(); + let list_of_statuses: Vec = (0u32..20).map(Status::Value).collect(); // ANCHOR_END: here } diff --git a/listings/ch20-web-server/listing-20-02/src/main.rs b/listings/ch20-web-server/listing-20-02/src/main.rs index 594246464..7dfdc2d89 100644 --- a/listings/ch20-web-server/listing-20-02/src/main.rs +++ b/listings/ch20-web-server/listing-20-02/src/main.rs @@ -1,6 +1,6 @@ use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; fn main() { let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); diff --git a/listings/ch20-web-server/listing-20-03/src/main.rs b/listings/ch20-web-server/listing-20-03/src/main.rs index c62319b6b..33f4fb732 100644 --- a/listings/ch20-web-server/listing-20-03/src/main.rs +++ b/listings/ch20-web-server/listing-20-03/src/main.rs @@ -1,6 +1,6 @@ use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; fn main() { let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); diff --git a/listings/ch20-web-server/listing-20-04/src/main.rs b/listings/ch20-web-server/listing-20-04/src/main.rs index b9739ba76..22e5d1da2 100644 --- a/listings/ch20-web-server/listing-20-04/src/main.rs +++ b/listings/ch20-web-server/listing-20-04/src/main.rs @@ -1,6 +1,6 @@ use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; fn main() { let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); diff --git a/listings/ch20-web-server/listing-20-05/src/main.rs b/listings/ch20-web-server/listing-20-05/src/main.rs index 073dafc00..0b3a284a7 100644 --- a/listings/ch20-web-server/listing-20-05/src/main.rs +++ b/listings/ch20-web-server/listing-20-05/src/main.rs @@ -4,8 +4,8 @@ use std::fs; // ANCHOR_END: here use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; fn main() { let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); diff --git a/listings/ch20-web-server/listing-20-06/src/main.rs b/listings/ch20-web-server/listing-20-06/src/main.rs index 3e894322c..dc192d76b 100644 --- a/listings/ch20-web-server/listing-20-06/src/main.rs +++ b/listings/ch20-web-server/listing-20-06/src/main.rs @@ -1,7 +1,7 @@ use std::fs; use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; fn main() { let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); diff --git a/listings/ch20-web-server/listing-20-07/src/main.rs b/listings/ch20-web-server/listing-20-07/src/main.rs index 06e80d0ff..9fab0c76b 100644 --- a/listings/ch20-web-server/listing-20-07/src/main.rs +++ b/listings/ch20-web-server/listing-20-07/src/main.rs @@ -1,7 +1,7 @@ use std::fs; use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; fn main() { let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); @@ -26,9 +26,8 @@ fn handle_connection(mut stream: TcpStream) { stream.write(response.as_bytes()).unwrap(); stream.flush().unwrap(); - // ANCHOR: here - // --snip-- - + // ANCHOR: here + // --snip-- } else { let status_line = "HTTP/1.1 404 NOT FOUND\r\n\r\n"; let contents = fs::read_to_string("404.html").unwrap(); diff --git a/listings/ch20-web-server/listing-20-08/src/main.rs b/listings/ch20-web-server/listing-20-08/src/main.rs index 00303a42a..cbcc4c01f 100644 --- a/listings/ch20-web-server/listing-20-08/src/main.rs +++ b/listings/ch20-web-server/listing-20-08/src/main.rs @@ -1,7 +1,7 @@ use std::fs; use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; fn main() { let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); diff --git a/listings/ch20-web-server/listing-20-09/src/main.rs b/listings/ch20-web-server/listing-20-09/src/main.rs index fbd2fbf7a..c91bb660d 100644 --- a/listings/ch20-web-server/listing-20-09/src/main.rs +++ b/listings/ch20-web-server/listing-20-09/src/main.rs @@ -1,7 +1,7 @@ use std::fs; use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; fn main() { let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); diff --git a/listings/ch20-web-server/listing-20-10/src/main.rs b/listings/ch20-web-server/listing-20-10/src/main.rs index 8fdbf5d48..6ea692fcb 100644 --- a/listings/ch20-web-server/listing-20-10/src/main.rs +++ b/listings/ch20-web-server/listing-20-10/src/main.rs @@ -1,7 +1,7 @@ use std::fs; use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; // ANCHOR: here use std::thread; use std::time::Duration; diff --git a/listings/ch20-web-server/listing-20-11/src/main.rs b/listings/ch20-web-server/listing-20-11/src/main.rs index 0d44625b1..7eb3372ba 100644 --- a/listings/ch20-web-server/listing-20-11/src/main.rs +++ b/listings/ch20-web-server/listing-20-11/src/main.rs @@ -1,7 +1,7 @@ use std::fs; use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; use std::thread; use std::time::Duration; diff --git a/listings/ch20-web-server/listing-20-12/src/main.rs b/listings/ch20-web-server/listing-20-12/src/main.rs index c0644f6ed..1381e6d6d 100644 --- a/listings/ch20-web-server/listing-20-12/src/main.rs +++ b/listings/ch20-web-server/listing-20-12/src/main.rs @@ -1,7 +1,7 @@ use std::fs; use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; use std::thread; use std::time::Duration; diff --git a/listings/ch20-web-server/listing-20-13/src/bin/main.rs b/listings/ch20-web-server/listing-20-13/src/bin/main.rs index f398eaed4..452f6822f 100644 --- a/listings/ch20-web-server/listing-20-13/src/bin/main.rs +++ b/listings/ch20-web-server/listing-20-13/src/bin/main.rs @@ -1,8 +1,8 @@ use hello::ThreadPool; use std::fs; use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; use std::thread; use std::time::Duration; diff --git a/listings/ch20-web-server/listing-20-13/src/lib.rs b/listings/ch20-web-server/listing-20-13/src/lib.rs index 1ce57a4e1..7c2b5ed62 100644 --- a/listings/ch20-web-server/listing-20-13/src/lib.rs +++ b/listings/ch20-web-server/listing-20-13/src/lib.rs @@ -19,8 +19,8 @@ impl ThreadPool { // ANCHOR_END: here pub fn execute(&self, f: F) - where - F: FnOnce() + Send + 'static + where + F: FnOnce() + Send + 'static, { } diff --git a/listings/ch20-web-server/listing-20-14/src/bin/main.rs b/listings/ch20-web-server/listing-20-14/src/bin/main.rs index f398eaed4..452f6822f 100644 --- a/listings/ch20-web-server/listing-20-14/src/bin/main.rs +++ b/listings/ch20-web-server/listing-20-14/src/bin/main.rs @@ -1,8 +1,8 @@ use hello::ThreadPool; use std::fs; use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; use std::thread; use std::time::Duration; diff --git a/listings/ch20-web-server/listing-20-14/src/lib.rs b/listings/ch20-web-server/listing-20-14/src/lib.rs index 9ce4b16ff..a42a882f7 100644 --- a/listings/ch20-web-server/listing-20-14/src/lib.rs +++ b/listings/ch20-web-server/listing-20-14/src/lib.rs @@ -25,17 +25,15 @@ impl ThreadPool { // create some threads and store them in the vector } - ThreadPool { - threads - } + ThreadPool { threads } } // --snip-- // ANCHOR_END: here pub fn execute(&self, f: F) - where - F: FnOnce() + Send + 'static + where + F: FnOnce() + Send + 'static, { } diff --git a/listings/ch20-web-server/listing-20-15/src/bin/main.rs b/listings/ch20-web-server/listing-20-15/src/bin/main.rs index f398eaed4..452f6822f 100644 --- a/listings/ch20-web-server/listing-20-15/src/bin/main.rs +++ b/listings/ch20-web-server/listing-20-15/src/bin/main.rs @@ -1,8 +1,8 @@ use hello::ThreadPool; use std::fs; use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; use std::thread; use std::time::Duration; diff --git a/listings/ch20-web-server/listing-20-15/src/lib.rs b/listings/ch20-web-server/listing-20-15/src/lib.rs index f1b1cd980..b81636e96 100644 --- a/listings/ch20-web-server/listing-20-15/src/lib.rs +++ b/listings/ch20-web-server/listing-20-15/src/lib.rs @@ -25,16 +25,14 @@ impl ThreadPool { workers.push(Worker::new(id)); } - ThreadPool { - workers - } + ThreadPool { workers } } // --snip-- // ANCHOR_END: here pub fn execute(&self, f: F) - where - F: FnOnce() + Send + 'static + where + F: FnOnce() + Send + 'static, { } @@ -50,10 +48,7 @@ impl Worker { fn new(id: usize) -> Worker { let thread = thread::spawn(|| {}); - Worker { - id, - thread, - } + Worker { id, thread } } } // ANCHOR_END: here diff --git a/listings/ch20-web-server/listing-20-16/src/bin/main.rs b/listings/ch20-web-server/listing-20-16/src/bin/main.rs index f398eaed4..452f6822f 100644 --- a/listings/ch20-web-server/listing-20-16/src/bin/main.rs +++ b/listings/ch20-web-server/listing-20-16/src/bin/main.rs @@ -1,8 +1,8 @@ use hello::ThreadPool; use std::fs; use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; use std::thread; use std::time::Duration; diff --git a/listings/ch20-web-server/listing-20-16/src/lib.rs b/listings/ch20-web-server/listing-20-16/src/lib.rs index 11591d31a..78ce6e41b 100644 --- a/listings/ch20-web-server/listing-20-16/src/lib.rs +++ b/listings/ch20-web-server/listing-20-16/src/lib.rs @@ -32,17 +32,14 @@ impl ThreadPool { workers.push(Worker::new(id)); } - ThreadPool { - workers, - sender, - } + ThreadPool { workers, sender } } // --snip-- // ANCHOR_END: here pub fn execute(&self, f: F) - where - F: FnOnce() + Send + 'static + where + F: FnOnce() + Send + 'static, { } @@ -59,10 +56,7 @@ impl Worker { fn new(id: usize) -> Worker { let thread = thread::spawn(|| {}); - Worker { - id, - thread, - } + Worker { id, thread } } } diff --git a/listings/ch20-web-server/listing-20-17/src/bin/main.rs b/listings/ch20-web-server/listing-20-17/src/bin/main.rs index f398eaed4..452f6822f 100644 --- a/listings/ch20-web-server/listing-20-17/src/bin/main.rs +++ b/listings/ch20-web-server/listing-20-17/src/bin/main.rs @@ -1,8 +1,8 @@ use hello::ThreadPool; use std::fs; use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; use std::thread; use std::time::Duration; diff --git a/listings/ch20-web-server/listing-20-17/src/lib.rs b/listings/ch20-web-server/listing-20-17/src/lib.rs index 80e34c368..6473ddcf5 100644 --- a/listings/ch20-web-server/listing-20-17/src/lib.rs +++ b/listings/ch20-web-server/listing-20-17/src/lib.rs @@ -1,5 +1,5 @@ -use std::thread; use std::sync::mpsc; +use std::thread; pub struct ThreadPool { workers: Vec, @@ -31,17 +31,14 @@ impl ThreadPool { workers.push(Worker::new(id, receiver)); } - ThreadPool { - workers, - sender, - } + ThreadPool { workers, sender } } // --snip-- // ANCHOR_END: here pub fn execute(&self, f: F) - where - F: FnOnce() + Send + 'static + where + F: FnOnce() + Send + 'static, { } @@ -64,10 +61,7 @@ impl Worker { receiver; }); - Worker { - id, - thread, - } + Worker { id, thread } } } // ANCHOR_END: here diff --git a/listings/ch20-web-server/listing-20-18/src/bin/main.rs b/listings/ch20-web-server/listing-20-18/src/bin/main.rs index f398eaed4..452f6822f 100644 --- a/listings/ch20-web-server/listing-20-18/src/bin/main.rs +++ b/listings/ch20-web-server/listing-20-18/src/bin/main.rs @@ -1,8 +1,8 @@ use hello::ThreadPool; use std::fs; use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; use std::thread; use std::time::Duration; diff --git a/listings/ch20-web-server/listing-20-18/src/lib.rs b/listings/ch20-web-server/listing-20-18/src/lib.rs index 6302b12c4..0fe4ec738 100644 --- a/listings/ch20-web-server/listing-20-18/src/lib.rs +++ b/listings/ch20-web-server/listing-20-18/src/lib.rs @@ -1,5 +1,5 @@ -use std::thread; use std::sync::mpsc; +use std::thread; // ANCHOR: here use std::sync::Arc; use std::sync::Mutex; @@ -38,18 +38,15 @@ impl ThreadPool { workers.push(Worker::new(id, Arc::clone(&receiver))); } - ThreadPool { - workers, - sender, - } + ThreadPool { workers, sender } } // --snip-- // ANCHOR_END: here pub fn execute(&self, f: F) - where - F: FnOnce() + Send + 'static + where + F: FnOnce() + Send + 'static, { } @@ -73,10 +70,7 @@ impl Worker { receiver; }); - Worker { - id, - thread, - } + Worker { id, thread } // ANCHOR: here } } diff --git a/listings/ch20-web-server/listing-20-19/src/bin/main.rs b/listings/ch20-web-server/listing-20-19/src/bin/main.rs index f398eaed4..452f6822f 100644 --- a/listings/ch20-web-server/listing-20-19/src/bin/main.rs +++ b/listings/ch20-web-server/listing-20-19/src/bin/main.rs @@ -1,8 +1,8 @@ use hello::ThreadPool; use std::fs; use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; use std::thread; use std::time::Duration; diff --git a/listings/ch20-web-server/listing-20-19/src/lib.rs b/listings/ch20-web-server/listing-20-19/src/lib.rs index 1b6841366..bf8127b8e 100644 --- a/listings/ch20-web-server/listing-20-19/src/lib.rs +++ b/listings/ch20-web-server/listing-20-19/src/lib.rs @@ -1,7 +1,7 @@ -use std::thread; use std::sync::mpsc; use std::sync::Arc; use std::sync::Mutex; +use std::thread; pub struct ThreadPool { workers: Vec, @@ -36,16 +36,13 @@ impl ThreadPool { workers.push(Worker::new(id, Arc::clone(&receiver))); } - ThreadPool { - workers, - sender, - } + ThreadPool { workers, sender } } // ANCHOR: here pub fn execute(&self, f: F) - where - F: FnOnce() + Send + 'static + where + F: FnOnce() + Send + 'static, { let job = Box::new(f); @@ -67,10 +64,7 @@ impl Worker { receiver; }); - Worker { - id, - thread, - } + Worker { id, thread } } } diff --git a/listings/ch20-web-server/listing-20-20/src/bin/main.rs b/listings/ch20-web-server/listing-20-20/src/bin/main.rs index f398eaed4..452f6822f 100644 --- a/listings/ch20-web-server/listing-20-20/src/bin/main.rs +++ b/listings/ch20-web-server/listing-20-20/src/bin/main.rs @@ -1,8 +1,8 @@ use hello::ThreadPool; use std::fs; use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; use std::thread; use std::time::Duration; diff --git a/listings/ch20-web-server/listing-20-20/src/lib.rs b/listings/ch20-web-server/listing-20-20/src/lib.rs index 71eb9c6f5..5fdc32e8b 100644 --- a/listings/ch20-web-server/listing-20-20/src/lib.rs +++ b/listings/ch20-web-server/listing-20-20/src/lib.rs @@ -1,7 +1,7 @@ -use std::thread; use std::sync::mpsc; use std::sync::Arc; use std::sync::Mutex; +use std::thread; pub struct ThreadPool { workers: Vec, @@ -31,15 +31,12 @@ impl ThreadPool { workers.push(Worker::new(id, Arc::clone(&receiver))); } - ThreadPool { - workers, - sender, - } + ThreadPool { workers, sender } } pub fn execute(&self, f: F) - where - F: FnOnce() + Send + 'static + where + F: FnOnce() + Send + 'static, { let job = Box::new(f); @@ -57,20 +54,15 @@ struct Worker { impl Worker { fn new(id: usize, receiver: Arc>>) -> Worker { - let thread = thread::spawn(move || { - loop { - let job = receiver.lock().unwrap().recv().unwrap(); + let thread = thread::spawn(move || loop { + let job = receiver.lock().unwrap().recv().unwrap(); - println!("Worker {} got a job; executing.", id); + println!("Worker {} got a job; executing.", id); - job(); - } + job(); }); - Worker { - id, - thread, - } + Worker { id, thread } } } // ANCHOR_END: here diff --git a/listings/ch20-web-server/listing-20-21/src/bin/main.rs b/listings/ch20-web-server/listing-20-21/src/bin/main.rs index f398eaed4..452f6822f 100644 --- a/listings/ch20-web-server/listing-20-21/src/bin/main.rs +++ b/listings/ch20-web-server/listing-20-21/src/bin/main.rs @@ -1,8 +1,8 @@ use hello::ThreadPool; use std::fs; use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; use std::thread; use std::time::Duration; diff --git a/listings/ch20-web-server/listing-20-21/src/lib.rs b/listings/ch20-web-server/listing-20-21/src/lib.rs index d55845696..6bde52488 100644 --- a/listings/ch20-web-server/listing-20-21/src/lib.rs +++ b/listings/ch20-web-server/listing-20-21/src/lib.rs @@ -1,7 +1,7 @@ -use std::thread; use std::sync::mpsc; use std::sync::Arc; use std::sync::Mutex; +use std::thread; pub struct ThreadPool { workers: Vec, @@ -31,15 +31,12 @@ impl ThreadPool { workers.push(Worker::new(id, Arc::clone(&receiver))); } - ThreadPool { - workers, - sender, - } + ThreadPool { workers, sender } } pub fn execute(&self, f: F) - where - F: FnOnce() + Send + 'static + where + F: FnOnce() + Send + 'static, { let job = Box::new(f); @@ -64,10 +61,7 @@ impl Worker { } }); - Worker { - id, - thread, - } + Worker { id, thread } } } // ANCHOR_END: here diff --git a/listings/ch20-web-server/listing-20-22/output.txt b/listings/ch20-web-server/listing-20-22/output.txt index 94ed27716..170d386f4 100644 --- a/listings/ch20-web-server/listing-20-22/output.txt +++ b/listings/ch20-web-server/listing-20-22/output.txt @@ -1,9 +1,9 @@ $ 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:55:13 + --> src/lib.rs:52:13 | -55 | worker.thread.join().unwrap(); +52 | worker.thread.join().unwrap(); | ^^^^^^^^^^^^^ move occurs because `worker.thread` has type `std::thread::JoinHandle<()>`, which does not implement the `Copy` trait error: aborting due to previous error diff --git a/listings/ch20-web-server/listing-20-22/src/bin/main.rs b/listings/ch20-web-server/listing-20-22/src/bin/main.rs index f398eaed4..452f6822f 100644 --- a/listings/ch20-web-server/listing-20-22/src/bin/main.rs +++ b/listings/ch20-web-server/listing-20-22/src/bin/main.rs @@ -1,8 +1,8 @@ use hello::ThreadPool; use std::fs; use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; use std::thread; use std::time::Duration; diff --git a/listings/ch20-web-server/listing-20-22/src/lib.rs b/listings/ch20-web-server/listing-20-22/src/lib.rs index 01d08c1cf..824257898 100644 --- a/listings/ch20-web-server/listing-20-22/src/lib.rs +++ b/listings/ch20-web-server/listing-20-22/src/lib.rs @@ -1,7 +1,7 @@ -use std::thread; use std::sync::mpsc; use std::sync::Arc; use std::sync::Mutex; +use std::thread; pub struct ThreadPool { workers: Vec, @@ -31,15 +31,12 @@ impl ThreadPool { workers.push(Worker::new(id, Arc::clone(&receiver))); } - ThreadPool { - workers, - sender, - } + ThreadPool { workers, sender } } pub fn execute(&self, f: F) - where - F: FnOnce() + Send + 'static + where + F: FnOnce() + Send + 'static, { let job = Box::new(f); @@ -66,19 +63,14 @@ struct Worker { impl Worker { fn new(id: usize, receiver: Arc>>) -> Worker { - let thread = thread::spawn(move || { - loop { - let job = receiver.lock().unwrap().recv().unwrap(); + let thread = thread::spawn(move || loop { + let job = receiver.lock().unwrap().recv().unwrap(); - println!("Worker {} got a job; executing.", id); + println!("Worker {} got a job; executing.", id); - job(); - } + job(); }); - Worker { - id, - thread, - } + Worker { id, thread } } } diff --git a/listings/ch20-web-server/listing-20-23/src/bin/main.rs b/listings/ch20-web-server/listing-20-23/src/bin/main.rs index f398eaed4..452f6822f 100644 --- a/listings/ch20-web-server/listing-20-23/src/bin/main.rs +++ b/listings/ch20-web-server/listing-20-23/src/bin/main.rs @@ -1,8 +1,8 @@ use hello::ThreadPool; use std::fs; use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; use std::thread; use std::time::Duration; diff --git a/listings/ch20-web-server/listing-20-23/src/lib.rs b/listings/ch20-web-server/listing-20-23/src/lib.rs index f590a44bf..6ccaee73f 100644 --- a/listings/ch20-web-server/listing-20-23/src/lib.rs +++ b/listings/ch20-web-server/listing-20-23/src/lib.rs @@ -1,7 +1,7 @@ -use std::thread; use std::sync::mpsc; use std::sync::Arc; use std::sync::Mutex; +use std::thread; // ANCHOR: here pub struct ThreadPool { @@ -44,16 +44,13 @@ impl ThreadPool { workers.push(Worker::new(id, Arc::clone(&receiver))); } - ThreadPool { - workers, - sender, - } + ThreadPool { workers, sender } } // ANCHOR: here pub fn execute(&self, f: F) - where - F: FnOnce() + Send + 'static + where + F: FnOnce() + Send + 'static, { let job = Box::new(f); @@ -83,24 +80,20 @@ struct Worker { // ANCHOR: here impl Worker { - fn new(id: usize, receiver: Arc>>) -> - Worker { - - let thread = thread::spawn(move ||{ - loop { - let message = receiver.lock().unwrap().recv().unwrap(); + fn new(id: usize, receiver: Arc>>) -> Worker { + let thread = thread::spawn(move || loop { + let message = receiver.lock().unwrap().recv().unwrap(); - match message { - Message::NewJob(job) => { - println!("Worker {} got a job; executing.", id); + match message { + Message::NewJob(job) => { + println!("Worker {} got a job; executing.", id); - job(); - }, - Message::Terminate => { - println!("Worker {} was told to terminate.", id); + job(); + } + Message::Terminate => { + println!("Worker {} was told to terminate.", id); - break; - }, + break; } } }); diff --git a/listings/ch20-web-server/listing-20-24/src/bin/main.rs b/listings/ch20-web-server/listing-20-24/src/bin/main.rs index f398eaed4..452f6822f 100644 --- a/listings/ch20-web-server/listing-20-24/src/bin/main.rs +++ b/listings/ch20-web-server/listing-20-24/src/bin/main.rs @@ -1,8 +1,8 @@ use hello::ThreadPool; use std::fs; use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; use std::thread; use std::time::Duration; diff --git a/listings/ch20-web-server/listing-20-24/src/lib.rs b/listings/ch20-web-server/listing-20-24/src/lib.rs index 319452f68..211d105b5 100644 --- a/listings/ch20-web-server/listing-20-24/src/lib.rs +++ b/listings/ch20-web-server/listing-20-24/src/lib.rs @@ -1,7 +1,7 @@ -use std::thread; use std::sync::mpsc; use std::sync::Arc; use std::sync::Mutex; +use std::thread; pub struct ThreadPool { workers: Vec, @@ -36,15 +36,12 @@ impl ThreadPool { workers.push(Worker::new(id, Arc::clone(&receiver))); } - ThreadPool { - workers, - sender, - } + ThreadPool { workers, sender } } pub fn execute(&self, f: F) - where - F: FnOnce() + Send + 'static + where + F: FnOnce() + Send + 'static, { let job = Box::new(f); @@ -80,24 +77,20 @@ struct Worker { } impl Worker { - fn new(id: usize, receiver: Arc>>) -> - Worker { - - let thread = thread::spawn(move ||{ - loop { - let message = receiver.lock().unwrap().recv().unwrap(); + fn new(id: usize, receiver: Arc>>) -> Worker { + let thread = thread::spawn(move || loop { + let message = receiver.lock().unwrap().recv().unwrap(); - match message { - Message::NewJob(job) => { - println!("Worker {} got a job; executing.", id); + match message { + Message::NewJob(job) => { + println!("Worker {} got a job; executing.", id); - job(); - }, - Message::Terminate => { - println!("Worker {} was told to terminate.", id); + job(); + } + Message::Terminate => { + println!("Worker {} was told to terminate.", id); - break; - }, + break; } } }); diff --git a/listings/ch20-web-server/listing-20-25/src/bin/main.rs b/listings/ch20-web-server/listing-20-25/src/bin/main.rs index 1ea317055..8d9310eae 100644 --- a/listings/ch20-web-server/listing-20-25/src/bin/main.rs +++ b/listings/ch20-web-server/listing-20-25/src/bin/main.rs @@ -2,8 +2,8 @@ use hello::ThreadPool; use std::fs; use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; use std::thread; use std::time::Duration; diff --git a/listings/ch20-web-server/listing-20-25/src/lib.rs b/listings/ch20-web-server/listing-20-25/src/lib.rs index 637d3ea62..ee52dd22c 100644 --- a/listings/ch20-web-server/listing-20-25/src/lib.rs +++ b/listings/ch20-web-server/listing-20-25/src/lib.rs @@ -1,8 +1,8 @@ // ANCHOR: here -use std::thread; use std::sync::mpsc; use std::sync::Arc; use std::sync::Mutex; +use std::thread; pub struct ThreadPool { workers: Vec, @@ -37,15 +37,12 @@ impl ThreadPool { workers.push(Worker::new(id, Arc::clone(&receiver))); } - ThreadPool { - workers, - sender, - } + ThreadPool { workers, sender } } pub fn execute(&self, f: F) - where - F: FnOnce() + Send + 'static + where + F: FnOnce() + Send + 'static, { let job = Box::new(f); @@ -79,24 +76,20 @@ struct Worker { } impl Worker { - fn new(id: usize, receiver: Arc>>) -> - Worker { - - let thread = thread::spawn(move ||{ - loop { - let message = receiver.lock().unwrap().recv().unwrap(); + fn new(id: usize, receiver: Arc>>) -> Worker { + let thread = thread::spawn(move || loop { + let message = receiver.lock().unwrap().recv().unwrap(); - match message { - Message::NewJob(job) => { - println!("Worker {} got a job; executing.", id); + match message { + Message::NewJob(job) => { + println!("Worker {} got a job; executing.", id); - job(); - }, - Message::Terminate => { - println!("Worker {} was told to terminate.", id); + job(); + } + Message::Terminate => { + println!("Worker {} was told to terminate.", id); - break; - }, + break; } } }); diff --git a/listings/ch20-web-server/no-listing-01-define-threadpool-struct/src/bin/main.rs b/listings/ch20-web-server/no-listing-01-define-threadpool-struct/src/bin/main.rs index 39e8c8fa0..d4e0e0c0e 100644 --- a/listings/ch20-web-server/no-listing-01-define-threadpool-struct/src/bin/main.rs +++ b/listings/ch20-web-server/no-listing-01-define-threadpool-struct/src/bin/main.rs @@ -3,8 +3,8 @@ use hello::ThreadPool; // ANCHOR_END: here use std::fs; use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; use std::thread; use std::time::Duration; diff --git a/listings/ch20-web-server/no-listing-02-impl-threadpool-new/src/bin/main.rs b/listings/ch20-web-server/no-listing-02-impl-threadpool-new/src/bin/main.rs index f398eaed4..452f6822f 100644 --- a/listings/ch20-web-server/no-listing-02-impl-threadpool-new/src/bin/main.rs +++ b/listings/ch20-web-server/no-listing-02-impl-threadpool-new/src/bin/main.rs @@ -1,8 +1,8 @@ use hello::ThreadPool; use std::fs; use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; use std::thread; use std::time::Duration; diff --git a/listings/ch20-web-server/no-listing-03-define-execute/src/bin/main.rs b/listings/ch20-web-server/no-listing-03-define-execute/src/bin/main.rs index f398eaed4..452f6822f 100644 --- a/listings/ch20-web-server/no-listing-03-define-execute/src/bin/main.rs +++ b/listings/ch20-web-server/no-listing-03-define-execute/src/bin/main.rs @@ -1,8 +1,8 @@ use hello::ThreadPool; use std::fs; use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; use std::thread; use std::time::Duration; diff --git a/listings/ch20-web-server/no-listing-03-define-execute/src/lib.rs b/listings/ch20-web-server/no-listing-03-define-execute/src/lib.rs index f3b4ef3fb..37f1d68e0 100644 --- a/listings/ch20-web-server/no-listing-03-define-execute/src/lib.rs +++ b/listings/ch20-web-server/no-listing-03-define-execute/src/lib.rs @@ -10,8 +10,8 @@ impl ThreadPool { // ANCHOR: here pub fn execute(&self, f: F) - where - F: FnOnce() + Send + 'static + where + F: FnOnce() + Send + 'static, { } diff --git a/listings/ch20-web-server/no-listing-04-update-worker-definition/output.txt b/listings/ch20-web-server/no-listing-04-update-worker-definition/output.txt index 5fa1d5bf5..6db89634b 100644 --- a/listings/ch20-web-server/no-listing-04-update-worker-definition/output.txt +++ b/listings/ch20-web-server/no-listing-04-update-worker-definition/output.txt @@ -1,19 +1,19 @@ $ cargo check Checking hello v0.1.0 (file:///projects/hello) error[E0599]: no method named `join` found for type `std::option::Option>` in the current scope - --> src/lib.rs:55:27 + --> src/lib.rs:52:27 | -55 | worker.thread.join().unwrap(); +52 | worker.thread.join().unwrap(); | ^^^^ error[E0308]: mismatched types - --> src/lib.rs:79:13 + --> src/lib.rs:72:22 | -79 | thread, - | ^^^^^^ - | | - | expected enum `std::option::Option`, found struct `std::thread::JoinHandle` - | help: try using a variant of the expected type: `Some(thread)` +72 | Worker { id, thread } + | ^^^^^^ + | | + | expected enum `std::option::Option`, found struct `std::thread::JoinHandle` + | help: try using a variant of the expected type: `Some(thread)` | = note: expected type `std::option::Option>` found type `std::thread::JoinHandle<_>` diff --git a/listings/ch20-web-server/no-listing-04-update-worker-definition/src/bin/main.rs b/listings/ch20-web-server/no-listing-04-update-worker-definition/src/bin/main.rs index f398eaed4..452f6822f 100644 --- a/listings/ch20-web-server/no-listing-04-update-worker-definition/src/bin/main.rs +++ b/listings/ch20-web-server/no-listing-04-update-worker-definition/src/bin/main.rs @@ -1,8 +1,8 @@ use hello::ThreadPool; use std::fs; use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; use std::thread; use std::time::Duration; diff --git a/listings/ch20-web-server/no-listing-04-update-worker-definition/src/lib.rs b/listings/ch20-web-server/no-listing-04-update-worker-definition/src/lib.rs index dd4d861a2..1098330b3 100644 --- a/listings/ch20-web-server/no-listing-04-update-worker-definition/src/lib.rs +++ b/listings/ch20-web-server/no-listing-04-update-worker-definition/src/lib.rs @@ -1,7 +1,7 @@ -use std::thread; use std::sync::mpsc; use std::sync::Arc; use std::sync::Mutex; +use std::thread; pub struct ThreadPool { workers: Vec, @@ -31,15 +31,12 @@ impl ThreadPool { workers.push(Worker::new(id, Arc::clone(&receiver))); } - ThreadPool { - workers, - sender, - } + ThreadPool { workers, sender } } pub fn execute(&self, f: F) - where - F: FnOnce() + Send + 'static + where + F: FnOnce() + Send + 'static, { let job = Box::new(f); @@ -66,19 +63,14 @@ struct Worker { impl Worker { fn new(id: usize, receiver: Arc>>) -> Worker { - let thread = thread::spawn(move || { - loop { - let job = receiver.lock().unwrap().recv().unwrap(); + let thread = thread::spawn(move || loop { + let job = receiver.lock().unwrap().recv().unwrap(); - println!("Worker {} got a job; executing.", id); + println!("Worker {} got a job; executing.", id); - job(); - } + job(); }); - Worker { - id, - thread, - } + Worker { id, thread } } } diff --git a/listings/ch20-web-server/no-listing-05-fix-worker-new/src/bin/main.rs b/listings/ch20-web-server/no-listing-05-fix-worker-new/src/bin/main.rs index f398eaed4..452f6822f 100644 --- a/listings/ch20-web-server/no-listing-05-fix-worker-new/src/bin/main.rs +++ b/listings/ch20-web-server/no-listing-05-fix-worker-new/src/bin/main.rs @@ -1,8 +1,8 @@ use hello::ThreadPool; use std::fs; use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; use std::thread; use std::time::Duration; diff --git a/listings/ch20-web-server/no-listing-05-fix-worker-new/src/lib.rs b/listings/ch20-web-server/no-listing-05-fix-worker-new/src/lib.rs index 012fd0a68..6bef23a16 100644 --- a/listings/ch20-web-server/no-listing-05-fix-worker-new/src/lib.rs +++ b/listings/ch20-web-server/no-listing-05-fix-worker-new/src/lib.rs @@ -1,7 +1,7 @@ -use std::thread; use std::sync::mpsc; use std::sync::Arc; use std::sync::Mutex; +use std::thread; pub struct ThreadPool { workers: Vec, @@ -31,15 +31,12 @@ impl ThreadPool { workers.push(Worker::new(id, Arc::clone(&receiver))); } - ThreadPool { - workers, - sender, - } + ThreadPool { workers, sender } } pub fn execute(&self, f: F) - where - F: FnOnce() + Send + 'static + where + F: FnOnce() + Send + 'static, { let job = Box::new(f); @@ -68,14 +65,12 @@ impl Worker { // --snip-- // ANCHOR_END: here - let thread = thread::spawn(move || { - loop { - let job = receiver.lock().unwrap().recv().unwrap(); + let thread = thread::spawn(move || loop { + let job = receiver.lock().unwrap().recv().unwrap(); - println!("Worker {} got a job; executing.", id); + println!("Worker {} got a job; executing.", id); - job(); - } + job(); }); // ANCHOR: here diff --git a/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/src/bin/main.rs b/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/src/bin/main.rs index f398eaed4..452f6822f 100644 --- a/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/src/bin/main.rs +++ b/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/src/bin/main.rs @@ -1,8 +1,8 @@ use hello::ThreadPool; use std::fs; use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; use std::thread; use std::time::Duration; diff --git a/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/src/lib.rs b/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/src/lib.rs index 5845a6fa4..d5b38a635 100644 --- a/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/src/lib.rs +++ b/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/src/lib.rs @@ -1,7 +1,7 @@ -use std::thread; use std::sync::mpsc; use std::sync::Arc; use std::sync::Mutex; +use std::thread; pub struct ThreadPool { workers: Vec, @@ -31,15 +31,12 @@ impl ThreadPool { workers.push(Worker::new(id, Arc::clone(&receiver))); } - ThreadPool { - workers, - sender, - } + ThreadPool { workers, sender } } pub fn execute(&self, f: F) - where - F: FnOnce() + Send + 'static + where + F: FnOnce() + Send + 'static, { let job = Box::new(f); @@ -68,14 +65,12 @@ struct Worker { impl Worker { fn new(id: usize, receiver: Arc>>) -> Worker { - let thread = thread::spawn(move || { - loop { - let job = receiver.lock().unwrap().recv().unwrap(); + let thread = thread::spawn(move || loop { + let job = receiver.lock().unwrap().recv().unwrap(); - println!("Worker {} got a job; executing.", id); + println!("Worker {} got a job; executing.", id); - job(); - } + job(); }); Worker { diff --git a/listings/ch20-web-server/no-listing-07-define-message-enum/src/bin/main.rs b/listings/ch20-web-server/no-listing-07-define-message-enum/src/bin/main.rs index f398eaed4..452f6822f 100644 --- a/listings/ch20-web-server/no-listing-07-define-message-enum/src/bin/main.rs +++ b/listings/ch20-web-server/no-listing-07-define-message-enum/src/bin/main.rs @@ -1,8 +1,8 @@ use hello::ThreadPool; use std::fs; use std::io::prelude::*; -use std::net::TcpStream; use std::net::TcpListener; +use std::net::TcpStream; use std::thread; use std::time::Duration; diff --git a/listings/ch20-web-server/no-listing-07-define-message-enum/src/lib.rs b/listings/ch20-web-server/no-listing-07-define-message-enum/src/lib.rs index 52c8b514d..ed1913b29 100644 --- a/listings/ch20-web-server/no-listing-07-define-message-enum/src/lib.rs +++ b/listings/ch20-web-server/no-listing-07-define-message-enum/src/lib.rs @@ -1,7 +1,7 @@ -use std::thread; use std::sync::mpsc; use std::sync::Arc; use std::sync::Mutex; +use std::thread; pub struct ThreadPool { workers: Vec, @@ -38,15 +38,12 @@ impl ThreadPool { workers.push(Worker::new(id, Arc::clone(&receiver))); } - ThreadPool { - workers, - sender, - } + ThreadPool { workers, sender } } pub fn execute(&self, f: F) - where - F: FnOnce() + Send + 'static + where + F: FnOnce() + Send + 'static, { let job = Box::new(f); @@ -73,14 +70,12 @@ struct Worker { impl Worker { fn new(id: usize, receiver: Arc>>) -> Worker { - let thread = thread::spawn(move || { - loop { - let job = receiver.lock().unwrap().recv().unwrap(); + let thread = thread::spawn(move || loop { + let job = receiver.lock().unwrap().recv().unwrap(); - println!("Worker {} got a job; executing.", id); + println!("Worker {} got a job; executing.", id); - job(); - } + job(); }); Worker { diff --git a/src/ch02-00-guessing-game-tutorial.md b/src/ch02-00-guessing-game-tutorial.md index 55055ca1b..a4ff0a2d4 100644 --- a/src/ch02-00-guessing-game-tutorial.md +++ b/src/ch02-00-guessing-game-tutorial.md @@ -69,7 +69,7 @@ allow the player to input a guess. Enter the code in Listing 2-1 into Filename: src/main.rs ```rust,ignore -{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs}} +{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:all}} ``` Listing 2-1: Code that gets a guess from the user and @@ -81,7 +81,7 @@ obtain user input and then print the result as output, we need to bring the standard library (which is known as `std`): ```rust,ignore -use std::io; +{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:io}} ``` By default, Rust brings only a few types into the scope of every program in @@ -96,7 +96,7 @@ As you saw in Chapter 1, the `main` function is the entry point into the program: ```rust,ignore -fn main() { +{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:main}} ``` The `fn` syntax declares a new function, the parentheses, `()`, indicate there @@ -106,9 +106,7 @@ As you also learned in Chapter 1, `println!` is a macro that prints a string to the screen: ```rust,ignore -println!("Guess the number!"); - -println!("Please input your guess."); +{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:print}} ``` This code is printing a prompt stating what the game is and requesting input @@ -119,7 +117,7 @@ from the user. Next, we’ll create a place to store the user input, like this: ```rust,ignore -let mut guess = String::new(); +{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:string}} ``` Now the program is getting interesting! There’s a lot going on in this little @@ -172,8 +170,7 @@ library with `use std::io;` on the first line of the program. Now we’ll call the `stdin` function from the `io` module: ```rust,ignore -io::stdin().read_line(&mut guess) - .expect("Failed to read line"); +{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:read}} ``` If we hadn’t put the `use std::io` line at the beginning of the program, we @@ -207,12 +204,12 @@ thoroughly.) ### Handling Potential Failure with the `Result` Type -We’re not quite done with this line of code. Although what we’ve discussed so -far is a single line of text, it’s only the first part of the single logical -line of code. The second part is this method: +We’re still working on this line of code. Although we’re now discussing a third +line of text, it’s still part of a single logical line of code. The next part +is this method: ```rust,ignore -.expect("Failed to read line"); +{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:expect}} ``` When you call a method with the `.foo()` syntax, it’s often wise to introduce a @@ -223,8 +220,8 @@ written this code as: io::stdin().read_line(&mut guess).expect("Failed to read line"); ``` -However, one long line is difficult to read, so it’s best to divide it: two -lines for two method calls. Now let’s discuss what this line does. +However, one long line is difficult to read, so it’s best to divide it. Now +let’s discuss what this line does. As mentioned earlier, `read_line` puts what the user types into the string we’re passing it, but it also returns a value—in this case, an @@ -276,11 +273,11 @@ because you just want to crash this program when a problem occurs, you can use ### Printing Values with `println!` Placeholders -Aside from the closing curly brackets, there’s only one more line to discuss in +Aside from the closing curly bracket, there’s only one more line to discuss in the code added so far, which is the following: ```rust,ignore -println!("You guessed: {}", guess); +{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:print_guess}} ``` This line prints the string we saved the user’s input in. The set of curly diff --git a/src/ch07-04-bringing-paths-into-scope-with-the-use-keyword.md b/src/ch07-04-bringing-paths-into-scope-with-the-use-keyword.md index 94af9db49..3bb5870b5 100644 --- a/src/ch07-04-bringing-paths-into-scope-with-the-use-keyword.md +++ b/src/ch07-04-bringing-paths-into-scope-with-the-use-keyword.md @@ -215,8 +215,8 @@ Listing 2-4 bring items from `std` into scope: Filename: src/main.rs -```rust -{{#include ../listings/ch07-managing-growing-projects/no-listing-01-use-std-unnested/src/main.rs:here}} +```rust,ignore +{{#rustdoc_include ../listings/ch07-managing-growing-projects/no-listing-01-use-std-unnested/src/main.rs:here}} ``` Instead, we can use nested paths to bring the same items into scope in one @@ -226,9 +226,8 @@ differ, as shown in Listing 7-18. Filename: src/main.rs -```rust -{{#include ../listings/ch07-managing-growing-projects/listing-07-18/src/main.rs:1}} -// --snip-- +```rust,ignore +{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-18/src/main.rs:here}} ``` Listing 7-18: Specifying a nested path to bring multiple