From a9fb154e1cd292e7310963302bad78ccf7d951d0 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Fri, 3 Jun 2022 16:16:02 -0400 Subject: [PATCH] More little improvements to chapter 12 - Change the variable name from filename to file_path. Fixes #3142. - Use the new println capture. Connects to #3048. - Use dbg! when appropriate. Connects to #2842. - Improve the expect message to read better. Connects to #2918. --- .../listing-12-01/src/main.rs | 2 +- .../listing-12-02/src/main.rs | 4 +- .../listing-12-03/src/main.rs | 4 +- .../listing-12-04/src/main.rs | 10 +- .../listing-12-05/src/main.rs | 14 +- .../listing-12-06/src/main.rs | 14 +- .../listing-12-07/src/main.rs | 14 +- .../listing-12-08/src/main.rs | 14 +- .../listing-12-09/src/main.rs | 14 +- .../listing-12-10/src/main.rs | 16 +-- .../listing-12-11/src/main.rs | 16 +-- .../listing-12-12/src/main.rs | 14 +- .../listing-12-13/src/lib.rs | 10 +- .../listing-12-13/src/main.rs | 6 +- .../listing-12-14/src/lib.rs | 10 +- .../listing-12-14/src/main.rs | 6 +- .../listing-12-15/src/lib.rs | 8 +- .../listing-12-15/src/main.rs | 4 +- .../listing-12-16/src/lib.rs | 8 +- .../listing-12-16/src/main.rs | 4 +- .../listing-12-17/src/lib.rs | 8 +- .../listing-12-17/src/main.rs | 4 +- .../listing-12-18/src/lib.rs | 8 +- .../listing-12-18/src/main.rs | 4 +- .../listing-12-19/src/lib.rs | 8 +- .../listing-12-19/src/main.rs | 4 +- .../listing-12-20/src/lib.rs | 10 +- .../listing-12-20/src/main.rs | 4 +- .../listing-12-21/src/lib.rs | 10 +- .../listing-12-21/src/main.rs | 4 +- .../listing-12-22/src/lib.rs | 10 +- .../listing-12-22/src/main.rs | 4 +- .../listing-12-23/src/lib.rs | 10 +- .../listing-12-23/src/main.rs | 4 +- .../listing-12-24/src/lib.rs | 10 +- .../listing-12-24/src/main.rs | 4 +- .../src/main.rs | 16 +-- .../src/lib.rs | 10 +- .../src/main.rs | 4 +- .../output-only-01-with-args/src/main.rs | 2 +- .../src/lib.rs | 8 +- .../src/main.rs | 4 +- .../src/lib.rs | 10 +- .../src/main.rs | 4 +- .../output-only-04-no-matches/src/lib.rs | 10 +- .../output-only-04-no-matches/src/main.rs | 4 +- .../listing-12-23-reproduced/src/lib.rs | 10 +- .../listing-12-23-reproduced/src/main.rs | 4 +- .../listing-12-24-reproduced/src/lib.rs | 10 +- .../listing-12-24-reproduced/src/main.rs | 4 +- .../listing-13-18/src/lib.rs | 10 +- .../listing-13-18/src/main.rs | 4 +- .../listing-13-19/src/lib.rs | 10 +- .../listing-13-19/src/main.rs | 4 +- .../listing-13-20/src/lib.rs | 12 +- .../listing-13-20/src/main.rs | 4 +- .../listing-13-22/src/lib.rs | 12 +- .../listing-13-22/src/main.rs | 4 +- nostarch/chapter12.md | 126 +++++++++--------- src/ch12-00-an-io-project.md | 4 +- ...h12-01-accepting-command-line-arguments.md | 12 +- src/ch12-02-reading-a-file.md | 4 +- ...improving-error-handling-and-modularity.md | 26 ++-- ...-06-writing-to-stderr-instead-of-stdout.md | 2 +- 64 files changed, 316 insertions(+), 316 deletions(-) diff --git a/listings/ch12-an-io-project/listing-12-01/src/main.rs b/listings/ch12-an-io-project/listing-12-01/src/main.rs index aa3056de60..ae7def53d6 100644 --- a/listings/ch12-an-io-project/listing-12-01/src/main.rs +++ b/listings/ch12-an-io-project/listing-12-01/src/main.rs @@ -2,5 +2,5 @@ use std::env; fn main() { let args: Vec = env::args().collect(); - println!("{:?}", args); + dbg!(args); } diff --git a/listings/ch12-an-io-project/listing-12-02/src/main.rs b/listings/ch12-an-io-project/listing-12-02/src/main.rs index 8daf125c67..ae2fa7bb1b 100644 --- a/listings/ch12-an-io-project/listing-12-02/src/main.rs +++ b/listings/ch12-an-io-project/listing-12-02/src/main.rs @@ -4,8 +4,8 @@ fn main() { let args: Vec = env::args().collect(); let query = &args[1]; - let filename = &args[2]; + let file_path = &args[2]; println!("Searching for {}", query); - println!("In file {}", filename); + println!("In file {}", file_path); } diff --git a/listings/ch12-an-io-project/listing-12-03/src/main.rs b/listings/ch12-an-io-project/listing-12-03/src/main.rs index 8daf125c67..ae2fa7bb1b 100644 --- a/listings/ch12-an-io-project/listing-12-03/src/main.rs +++ b/listings/ch12-an-io-project/listing-12-03/src/main.rs @@ -4,8 +4,8 @@ fn main() { let args: Vec = env::args().collect(); let query = &args[1]; - let filename = &args[2]; + let file_path = &args[2]; println!("Searching for {}", query); - println!("In file {}", filename); + println!("In file {}", file_path); } diff --git a/listings/ch12-an-io-project/listing-12-04/src/main.rs b/listings/ch12-an-io-project/listing-12-04/src/main.rs index 9a0eade562..944e4300ec 100644 --- a/listings/ch12-an-io-project/listing-12-04/src/main.rs +++ b/listings/ch12-an-io-project/listing-12-04/src/main.rs @@ -8,15 +8,15 @@ fn main() { let args: Vec = env::args().collect(); let query = &args[1]; - let filename = &args[2]; + let file_path = &args[2]; println!("Searching for {}", query); // ANCHOR: here - println!("In file {}", filename); + println!("In file {}", file_path); - let contents = fs::read_to_string(filename) - .expect("Something went wrong reading the file"); + let contents = fs::read_to_string(file_path) + .expect("Should have been able to read the file"); - println!("With text:\n{}", contents); + println!("With text:\n{contents}"); } // ANCHOR_END: here diff --git a/listings/ch12-an-io-project/listing-12-05/src/main.rs b/listings/ch12-an-io-project/listing-12-05/src/main.rs index fb8805687e..0615918338 100644 --- a/listings/ch12-an-io-project/listing-12-05/src/main.rs +++ b/listings/ch12-an-io-project/listing-12-05/src/main.rs @@ -5,25 +5,25 @@ use std::fs; fn main() { let args: Vec = env::args().collect(); - let (query, filename) = parse_config(&args); + let (query, file_path) = parse_config(&args); // --snip-- // ANCHOR_END: here println!("Searching for {}", query); - println!("In file {}", filename); + println!("In file {}", file_path); - let contents = fs::read_to_string(filename) - .expect("Something went wrong reading the file"); + let contents = fs::read_to_string(file_path) + .expect("Should have been able to read the file"); - println!("With text:\n{}", contents); + println!("With text:\n{contents}"); // ANCHOR: here } fn parse_config(args: &[String]) -> (&str, &str) { let query = &args[1]; - let filename = &args[2]; + let file_path = &args[2]; - (query, filename) + (query, file_path) } // ANCHOR_END: here diff --git a/listings/ch12-an-io-project/listing-12-06/src/main.rs b/listings/ch12-an-io-project/listing-12-06/src/main.rs index b5d89bc98f..c77e848d80 100644 --- a/listings/ch12-an-io-project/listing-12-06/src/main.rs +++ b/listings/ch12-an-io-project/listing-12-06/src/main.rs @@ -8,27 +8,27 @@ fn main() { let config = parse_config(&args); println!("Searching for {}", config.query); - println!("In file {}", config.filename); + println!("In file {}", config.file_path); - let contents = fs::read_to_string(config.filename) - .expect("Something went wrong reading the file"); + let contents = fs::read_to_string(config.file_path) + .expect("Should have been able to read the file"); // --snip-- // ANCHOR_END: here - println!("With text:\n{}", contents); + println!("With text:\n{contents}"); // ANCHOR: here } struct Config { query: String, - filename: String, + file_path: String, } fn parse_config(args: &[String]) -> Config { let query = args[1].clone(); - let filename = args[2].clone(); + let file_path = args[2].clone(); - Config { query, filename } + Config { query, file_path } } // ANCHOR_END: here diff --git a/listings/ch12-an-io-project/listing-12-07/src/main.rs b/listings/ch12-an-io-project/listing-12-07/src/main.rs index 36d35ceafb..ff6c294200 100644 --- a/listings/ch12-an-io-project/listing-12-07/src/main.rs +++ b/listings/ch12-an-io-project/listing-12-07/src/main.rs @@ -9,12 +9,12 @@ fn main() { // ANCHOR_END: here println!("Searching for {}", config.query); - println!("In file {}", config.filename); + println!("In file {}", config.file_path); - let contents = fs::read_to_string(config.filename) - .expect("Something went wrong reading the file"); + let contents = fs::read_to_string(config.file_path) + .expect("Should have been able to read the file"); - println!("With text:\n{}", contents); + println!("With text:\n{contents}"); // ANCHOR: here // --snip-- @@ -25,16 +25,16 @@ fn main() { // ANCHOR_END: here struct Config { query: String, - filename: String, + file_path: String, } // ANCHOR: here impl Config { fn new(args: &[String]) -> Config { let query = args[1].clone(); - let filename = args[2].clone(); + let file_path = args[2].clone(); - Config { query, filename } + Config { query, file_path } } } // ANCHOR_END: here 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 dddf10bd44..cecd15abf9 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 @@ -7,17 +7,17 @@ fn main() { let config = Config::new(&args); println!("Searching for {}", config.query); - println!("In file {}", config.filename); + println!("In file {}", config.file_path); - let contents = fs::read_to_string(config.filename) - .expect("Something went wrong reading the file"); + let contents = fs::read_to_string(config.file_path) + .expect("Should have been able to read the file"); - println!("With text:\n{}", contents); + println!("With text:\n{contents}"); } struct Config { query: String, - filename: String, + file_path: String, } impl Config { @@ -31,8 +31,8 @@ impl Config { // ANCHOR_END: here let query = args[1].clone(); - let filename = args[2].clone(); + let file_path = args[2].clone(); - Config { query, filename } + Config { query, file_path } } } diff --git a/listings/ch12-an-io-project/listing-12-09/src/main.rs b/listings/ch12-an-io-project/listing-12-09/src/main.rs index 304d0141ff..3418e718c1 100644 --- a/listings/ch12-an-io-project/listing-12-09/src/main.rs +++ b/listings/ch12-an-io-project/listing-12-09/src/main.rs @@ -7,17 +7,17 @@ fn main() { let config = Config::new(&args); println!("Searching for {}", config.query); - println!("In file {}", config.filename); + println!("In file {}", config.file_path); - let contents = fs::read_to_string(config.filename) - .expect("Something went wrong reading the file"); + let contents = fs::read_to_string(config.file_path) + .expect("Should have been able to read the file"); - println!("With text:\n{}", contents); + println!("With text:\n{contents}"); } struct Config { query: String, - filename: String, + file_path: String, } // ANCHOR: here @@ -28,9 +28,9 @@ impl Config { } let query = args[1].clone(); - let filename = args[2].clone(); + let file_path = args[2].clone(); - Ok(Config { query, filename }) + Ok(Config { query, file_path }) } } // ANCHOR_END: here diff --git a/listings/ch12-an-io-project/listing-12-10/src/main.rs b/listings/ch12-an-io-project/listing-12-10/src/main.rs index 67d88c498c..ab6d3e5487 100644 --- a/listings/ch12-an-io-project/listing-12-10/src/main.rs +++ b/listings/ch12-an-io-project/listing-12-10/src/main.rs @@ -7,7 +7,7 @@ fn main() { let args: Vec = env::args().collect(); let config = Config::build(&args).unwrap_or_else(|err| { - println!("Problem parsing arguments: {}", err); + println!("Problem parsing arguments: {err}"); process::exit(1); }); @@ -15,17 +15,17 @@ fn main() { // ANCHOR_END: here println!("Searching for {}", config.query); - println!("In file {}", config.filename); + println!("In file {}", config.file_path); - let contents = fs::read_to_string(config.filename) - .expect("Something went wrong reading the file"); + let contents = fs::read_to_string(config.file_path) + .expect("Should have been able to read the file"); - println!("With text:\n{}", contents); + println!("With text:\n{contents}"); } struct Config { query: String, - filename: String, + file_path: String, } impl Config { @@ -35,8 +35,8 @@ impl Config { } let query = args[1].clone(); - let filename = args[2].clone(); + let file_path = args[2].clone(); - Ok(Config { query, filename }) + Ok(Config { query, file_path }) } } diff --git a/listings/ch12-an-io-project/listing-12-11/src/main.rs b/listings/ch12-an-io-project/listing-12-11/src/main.rs index 65cec174a5..3f476b9751 100644 --- a/listings/ch12-an-io-project/listing-12-11/src/main.rs +++ b/listings/ch12-an-io-project/listing-12-11/src/main.rs @@ -10,22 +10,22 @@ fn main() { let args: Vec = env::args().collect(); let config = Config::build(&args).unwrap_or_else(|err| { - println!("Problem parsing arguments: {}", err); + println!("Problem parsing arguments: {err}"); process::exit(1); }); // ANCHOR: here println!("Searching for {}", config.query); - println!("In file {}", config.filename); + println!("In file {}", config.file_path); run(config); } fn run(config: Config) { - let contents = fs::read_to_string(config.filename) - .expect("Something went wrong reading the file"); + let contents = fs::read_to_string(config.file_path) + .expect("Should have been able to read the file"); - println!("With text:\n{}", contents); + println!("With text:\n{contents}"); } // --snip-- @@ -33,7 +33,7 @@ fn run(config: Config) { struct Config { query: String, - filename: String, + file_path: String, } impl Config { @@ -43,8 +43,8 @@ impl Config { } let query = args[1].clone(); - let filename = args[2].clone(); + let file_path = args[2].clone(); - Ok(Config { query, filename }) + Ok(Config { query, file_path }) } } diff --git a/listings/ch12-an-io-project/listing-12-12/src/main.rs b/listings/ch12-an-io-project/listing-12-12/src/main.rs index 52d4a7e43d..ab5fdb894d 100644 --- a/listings/ch12-an-io-project/listing-12-12/src/main.rs +++ b/listings/ch12-an-io-project/listing-12-12/src/main.rs @@ -12,21 +12,21 @@ fn main() { let args: Vec = env::args().collect(); let config = Config::build(&args).unwrap_or_else(|err| { - println!("Problem parsing arguments: {}", err); + println!("Problem parsing arguments: {err}"); process::exit(1); }); println!("Searching for {}", config.query); - println!("In file {}", config.filename); + println!("In file {}", config.file_path); run(config); } // ANCHOR: here fn run(config: Config) -> Result<(), Box> { - let contents = fs::read_to_string(config.filename)?; + let contents = fs::read_to_string(config.file_path)?; - println!("With text:\n{}", contents); + println!("With text:\n{contents}"); Ok(()) } @@ -34,7 +34,7 @@ fn run(config: Config) -> Result<(), Box> { struct Config { query: String, - filename: String, + file_path: String, } impl Config { @@ -44,8 +44,8 @@ impl Config { } let query = args[1].clone(); - let filename = args[2].clone(); + let file_path = args[2].clone(); - Ok(Config { query, filename }) + Ok(Config { query, file_path }) } } diff --git a/listings/ch12-an-io-project/listing-12-13/src/lib.rs b/listings/ch12-an-io-project/listing-12-13/src/lib.rs index 476cc12726..1a3c480893 100644 --- a/listings/ch12-an-io-project/listing-12-13/src/lib.rs +++ b/listings/ch12-an-io-project/listing-12-13/src/lib.rs @@ -4,7 +4,7 @@ use std::fs; pub struct Config { pub query: String, - pub filename: String, + pub file_path: String, } impl Config { @@ -16,9 +16,9 @@ impl Config { } let query = args[1].clone(); - let filename = args[2].clone(); + let file_path = args[2].clone(); - Ok(Config { query, filename }) + Ok(Config { query, file_path }) // ANCHOR: here } } @@ -26,9 +26,9 @@ impl Config { pub fn run(config: Config) -> Result<(), Box> { // --snip-- // ANCHOR_END: here - let contents = fs::read_to_string(config.filename)?; + let contents = fs::read_to_string(config.file_path)?; - println!("With text:\n{}", contents); + println!("With text:\n{contents}"); Ok(()) // ANCHOR: here diff --git a/listings/ch12-an-io-project/listing-12-13/src/main.rs b/listings/ch12-an-io-project/listing-12-13/src/main.rs index de0701a74a..03d8e9584c 100644 --- a/listings/ch12-an-io-project/listing-12-13/src/main.rs +++ b/listings/ch12-an-io-project/listing-12-13/src/main.rs @@ -5,15 +5,15 @@ fn main() { let args: Vec = env::args().collect(); let config = Config::build(&args).unwrap_or_else(|err| { - println!("Problem parsing arguments: {}", err); + println!("Problem parsing arguments: {err}"); process::exit(1); }); println!("Searching for {}", config.query); - println!("In file {}", config.filename); + println!("In file {}", config.file_path); if let Err(e) = run(config) { - println!("Application error: {}", e); + println!("Application error: {e}"); process::exit(1); } diff --git a/listings/ch12-an-io-project/listing-12-14/src/lib.rs b/listings/ch12-an-io-project/listing-12-14/src/lib.rs index 17cf19f77d..4f3a4e865b 100644 --- a/listings/ch12-an-io-project/listing-12-14/src/lib.rs +++ b/listings/ch12-an-io-project/listing-12-14/src/lib.rs @@ -3,7 +3,7 @@ use std::fs; pub struct Config { pub query: String, - pub filename: String, + pub file_path: String, } impl Config { @@ -13,16 +13,16 @@ impl Config { } let query = args[1].clone(); - let filename = args[2].clone(); + let file_path = args[2].clone(); - Ok(Config { query, filename }) + Ok(Config { query, file_path }) } } pub fn run(config: Config) -> Result<(), Box> { - let contents = fs::read_to_string(config.filename)?; + let contents = fs::read_to_string(config.file_path)?; - println!("With text:\n{}", contents); + println!("With text:\n{contents}"); Ok(()) } diff --git a/listings/ch12-an-io-project/listing-12-14/src/main.rs b/listings/ch12-an-io-project/listing-12-14/src/main.rs index d0fceaca37..110c9bfda8 100644 --- a/listings/ch12-an-io-project/listing-12-14/src/main.rs +++ b/listings/ch12-an-io-project/listing-12-14/src/main.rs @@ -10,18 +10,18 @@ fn main() { let args: Vec = env::args().collect(); let config = Config::build(&args).unwrap_or_else(|err| { - println!("Problem parsing arguments: {}", err); + println!("Problem parsing arguments: {err}"); process::exit(1); }); println!("Searching for {}", config.query); - println!("In file {}", config.filename); + println!("In file {}", config.file_path); // ANCHOR: here if let Err(e) = minigrep::run(config) { // --snip-- // ANCHOR_END: here - println!("Application error: {}", e); + println!("Application error: {e}"); process::exit(1); // ANCHOR: here 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 ecce928cbf..20c4a782b9 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 @@ -3,7 +3,7 @@ use std::fs; pub struct Config { pub query: String, - pub filename: String, + pub file_path: String, } impl Config { @@ -13,14 +13,14 @@ impl Config { } let query = args[1].clone(); - let filename = args[2].clone(); + let file_path = args[2].clone(); - Ok(Config { query, filename }) + Ok(Config { query, file_path }) } } pub fn run(config: Config) -> Result<(), Box> { - let contents = fs::read_to_string(config.filename)?; + let contents = fs::read_to_string(config.file_path)?; Ok(()) } diff --git a/listings/ch12-an-io-project/listing-12-15/src/main.rs b/listings/ch12-an-io-project/listing-12-15/src/main.rs index 5f362421a7..e355a4fb94 100644 --- a/listings/ch12-an-io-project/listing-12-15/src/main.rs +++ b/listings/ch12-an-io-project/listing-12-15/src/main.rs @@ -7,12 +7,12 @@ fn main() { let args: Vec = env::args().collect(); let config = Config::build(&args).unwrap_or_else(|err| { - println!("Problem parsing arguments: {}", err); + println!("Problem parsing arguments: {err}"); process::exit(1); }); if let Err(e) = minigrep::run(config) { - println!("Application error: {}", e); + println!("Application error: {e}"); process::exit(1); } 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 e53406a700..f5e593484a 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 @@ -3,7 +3,7 @@ use std::fs; pub struct Config { pub query: String, - pub filename: String, + pub file_path: String, } impl Config { @@ -13,14 +13,14 @@ impl Config { } let query = args[1].clone(); - let filename = args[2].clone(); + let file_path = args[2].clone(); - Ok(Config { query, filename }) + Ok(Config { query, file_path }) } } pub fn run(config: Config) -> Result<(), Box> { - let contents = fs::read_to_string(config.filename)?; + let contents = fs::read_to_string(config.file_path)?; Ok(()) } diff --git a/listings/ch12-an-io-project/listing-12-16/src/main.rs b/listings/ch12-an-io-project/listing-12-16/src/main.rs index 5f362421a7..e355a4fb94 100644 --- a/listings/ch12-an-io-project/listing-12-16/src/main.rs +++ b/listings/ch12-an-io-project/listing-12-16/src/main.rs @@ -7,12 +7,12 @@ fn main() { let args: Vec = env::args().collect(); let config = Config::build(&args).unwrap_or_else(|err| { - println!("Problem parsing arguments: {}", err); + println!("Problem parsing arguments: {err}"); process::exit(1); }); if let Err(e) = minigrep::run(config) { - println!("Application error: {}", e); + println!("Application error: {e}"); process::exit(1); } 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 2bdd4628c6..cb9fab4016 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 @@ -3,7 +3,7 @@ use std::fs; pub struct Config { pub query: String, - pub filename: String, + pub file_path: String, } impl Config { @@ -13,14 +13,14 @@ impl Config { } let query = args[1].clone(); - let filename = args[2].clone(); + let file_path = args[2].clone(); - Ok(Config { query, filename }) + Ok(Config { query, file_path }) } } pub fn run(config: Config) -> Result<(), Box> { - let contents = fs::read_to_string(config.filename)?; + let contents = fs::read_to_string(config.file_path)?; Ok(()) } diff --git a/listings/ch12-an-io-project/listing-12-17/src/main.rs b/listings/ch12-an-io-project/listing-12-17/src/main.rs index 5f362421a7..e355a4fb94 100644 --- a/listings/ch12-an-io-project/listing-12-17/src/main.rs +++ b/listings/ch12-an-io-project/listing-12-17/src/main.rs @@ -7,12 +7,12 @@ fn main() { let args: Vec = env::args().collect(); let config = Config::build(&args).unwrap_or_else(|err| { - println!("Problem parsing arguments: {}", err); + println!("Problem parsing arguments: {err}"); process::exit(1); }); if let Err(e) = minigrep::run(config) { - println!("Application error: {}", e); + println!("Application error: {e}"); process::exit(1); } 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 112a6aaac5..a05d046d7d 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 @@ -3,7 +3,7 @@ use std::fs; pub struct Config { pub query: String, - pub filename: String, + pub file_path: String, } impl Config { @@ -13,14 +13,14 @@ impl Config { } let query = args[1].clone(); - let filename = args[2].clone(); + let file_path = args[2].clone(); - Ok(Config { query, filename }) + Ok(Config { query, file_path }) } } pub fn run(config: Config) -> Result<(), Box> { - let contents = fs::read_to_string(config.filename)?; + let contents = fs::read_to_string(config.file_path)?; Ok(()) } diff --git a/listings/ch12-an-io-project/listing-12-18/src/main.rs b/listings/ch12-an-io-project/listing-12-18/src/main.rs index 5f362421a7..e355a4fb94 100644 --- a/listings/ch12-an-io-project/listing-12-18/src/main.rs +++ b/listings/ch12-an-io-project/listing-12-18/src/main.rs @@ -7,12 +7,12 @@ fn main() { let args: Vec = env::args().collect(); let config = Config::build(&args).unwrap_or_else(|err| { - println!("Problem parsing arguments: {}", err); + println!("Problem parsing arguments: {err}"); process::exit(1); }); if let Err(e) = minigrep::run(config) { - println!("Application error: {}", e); + println!("Application error: {e}"); process::exit(1); } 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 7dc117203b..f5d3ffa9f6 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 @@ -3,7 +3,7 @@ use std::fs; pub struct Config { pub query: String, - pub filename: String, + pub file_path: String, } impl Config { @@ -13,14 +13,14 @@ impl Config { } let query = args[1].clone(); - let filename = args[2].clone(); + let file_path = args[2].clone(); - Ok(Config { query, filename }) + Ok(Config { query, file_path }) } } pub fn run(config: Config) -> Result<(), Box> { - let contents = fs::read_to_string(config.filename)?; + let contents = fs::read_to_string(config.file_path)?; Ok(()) } diff --git a/listings/ch12-an-io-project/listing-12-19/src/main.rs b/listings/ch12-an-io-project/listing-12-19/src/main.rs index 5f362421a7..e355a4fb94 100644 --- a/listings/ch12-an-io-project/listing-12-19/src/main.rs +++ b/listings/ch12-an-io-project/listing-12-19/src/main.rs @@ -7,12 +7,12 @@ fn main() { let args: Vec = env::args().collect(); let config = Config::build(&args).unwrap_or_else(|err| { - println!("Problem parsing arguments: {}", err); + println!("Problem parsing arguments: {err}"); process::exit(1); }); if let Err(e) = minigrep::run(config) { - println!("Application error: {}", e); + println!("Application error: {e}"); process::exit(1); } 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 06069ec02b..a757f7f550 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 @@ -3,7 +3,7 @@ use std::fs; pub struct Config { pub query: String, - pub filename: String, + pub file_path: String, } impl Config { @@ -13,17 +13,17 @@ impl Config { } let query = args[1].clone(); - let filename = args[2].clone(); + let file_path = args[2].clone(); - Ok(Config { query, filename }) + Ok(Config { query, file_path }) } } pub fn run(config: Config) -> Result<(), Box> { - let contents = fs::read_to_string(config.filename)?; + let contents = fs::read_to_string(config.file_path)?; for line in search(&config.query, &contents) { - println!("{}", line); + println!("{line}"); } Ok(()) diff --git a/listings/ch12-an-io-project/listing-12-20/src/main.rs b/listings/ch12-an-io-project/listing-12-20/src/main.rs index 5f362421a7..e355a4fb94 100644 --- a/listings/ch12-an-io-project/listing-12-20/src/main.rs +++ b/listings/ch12-an-io-project/listing-12-20/src/main.rs @@ -7,12 +7,12 @@ fn main() { let args: Vec = env::args().collect(); let config = Config::build(&args).unwrap_or_else(|err| { - println!("Problem parsing arguments: {}", err); + println!("Problem parsing arguments: {err}"); process::exit(1); }); if let Err(e) = minigrep::run(config) { - println!("Application error: {}", e); + println!("Application error: {e}"); process::exit(1); } 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 b1c0f3d22a..3aaa04082c 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 @@ -3,7 +3,7 @@ use std::fs; pub struct Config { pub query: String, - pub filename: String, + pub file_path: String, } impl Config { @@ -13,17 +13,17 @@ impl Config { } let query = args[1].clone(); - let filename = args[2].clone(); + let file_path = args[2].clone(); - Ok(Config { query, filename }) + Ok(Config { query, file_path }) } } pub fn run(config: Config) -> Result<(), Box> { - let contents = fs::read_to_string(config.filename)?; + let contents = fs::read_to_string(config.file_path)?; for line in search(&config.query, &contents) { - println!("{}", line); + println!("{line}"); } Ok(()) diff --git a/listings/ch12-an-io-project/listing-12-21/src/main.rs b/listings/ch12-an-io-project/listing-12-21/src/main.rs index 5f362421a7..e355a4fb94 100644 --- a/listings/ch12-an-io-project/listing-12-21/src/main.rs +++ b/listings/ch12-an-io-project/listing-12-21/src/main.rs @@ -7,12 +7,12 @@ fn main() { let args: Vec = env::args().collect(); let config = Config::build(&args).unwrap_or_else(|err| { - println!("Problem parsing arguments: {}", err); + println!("Problem parsing arguments: {err}"); process::exit(1); }); if let Err(e) = minigrep::run(config) { - println!("Application error: {}", e); + println!("Application error: {e}"); process::exit(1); } 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 a1252181b2..c3f4723f10 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 @@ -4,7 +4,7 @@ use std::fs; // ANCHOR: here pub struct Config { pub query: String, - pub filename: String, + pub file_path: String, pub ignore_case: bool, } // ANCHOR_END: here @@ -16,15 +16,15 @@ impl Config { } let query = args[1].clone(); - let filename = args[2].clone(); + let file_path = args[2].clone(); - Ok(Config { query, filename }) + Ok(Config { query, file_path }) } } // ANCHOR: there pub fn run(config: Config) -> Result<(), Box> { - let contents = fs::read_to_string(config.filename)?; + let contents = fs::read_to_string(config.file_path)?; let results = if config.ignore_case { search_case_insensitive(&config.query, &contents) @@ -33,7 +33,7 @@ pub fn run(config: Config) -> Result<(), Box> { }; for line in results { - println!("{}", line); + println!("{line}"); } Ok(()) diff --git a/listings/ch12-an-io-project/listing-12-22/src/main.rs b/listings/ch12-an-io-project/listing-12-22/src/main.rs index 5f362421a7..e355a4fb94 100644 --- a/listings/ch12-an-io-project/listing-12-22/src/main.rs +++ b/listings/ch12-an-io-project/listing-12-22/src/main.rs @@ -7,12 +7,12 @@ fn main() { let args: Vec = env::args().collect(); let config = Config::build(&args).unwrap_or_else(|err| { - println!("Problem parsing arguments: {}", err); + println!("Problem parsing arguments: {err}"); process::exit(1); }); if let Err(e) = minigrep::run(config) { - println!("Application error: {}", e); + println!("Application error: {e}"); process::exit(1); } 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 fb026d0ba6..20eda21975 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 @@ -8,7 +8,7 @@ use std::fs; pub struct Config { pub query: String, - pub filename: String, + pub file_path: String, pub ignore_case: bool, } @@ -20,13 +20,13 @@ impl Config { } let query = args[1].clone(); - let filename = args[2].clone(); + let file_path = args[2].clone(); let ignore_case = env::var("IGNORE_CASE").is_ok(); Ok(Config { query, - filename, + file_path, ignore_case, }) } @@ -34,7 +34,7 @@ impl Config { // ANCHOR_END: here pub fn run(config: Config) -> Result<(), Box> { - let contents = fs::read_to_string(config.filename)?; + let contents = fs::read_to_string(config.file_path)?; let results = if config.ignore_case { search_case_insensitive(&config.query, &contents) @@ -43,7 +43,7 @@ pub fn run(config: Config) -> Result<(), Box> { }; for line in results { - println!("{}", line); + println!("{line}"); } Ok(()) diff --git a/listings/ch12-an-io-project/listing-12-23/src/main.rs b/listings/ch12-an-io-project/listing-12-23/src/main.rs index 5f362421a7..e355a4fb94 100644 --- a/listings/ch12-an-io-project/listing-12-23/src/main.rs +++ b/listings/ch12-an-io-project/listing-12-23/src/main.rs @@ -7,12 +7,12 @@ fn main() { let args: Vec = env::args().collect(); let config = Config::build(&args).unwrap_or_else(|err| { - println!("Problem parsing arguments: {}", err); + println!("Problem parsing arguments: {err}"); process::exit(1); }); if let Err(e) = minigrep::run(config) { - println!("Application error: {}", e); + println!("Application error: {e}"); process::exit(1); } 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 511b0136f2..292b097897 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 @@ -4,7 +4,7 @@ use std::fs; pub struct Config { pub query: String, - pub filename: String, + pub file_path: String, pub ignore_case: bool, } @@ -15,20 +15,20 @@ impl Config { } let query = args[1].clone(); - let filename = args[2].clone(); + let file_path = args[2].clone(); let ignore_case = env::var("IGNORE_CASE").is_ok(); Ok(Config { query, - filename, + file_path, ignore_case, }) } } pub fn run(config: Config) -> Result<(), Box> { - let contents = fs::read_to_string(config.filename)?; + let contents = fs::read_to_string(config.file_path)?; let results = if config.ignore_case { search_case_insensitive(&config.query, &contents) @@ -37,7 +37,7 @@ pub fn run(config: Config) -> Result<(), Box> { }; for line in results { - println!("{}", line); + println!("{line}"); } Ok(()) diff --git a/listings/ch12-an-io-project/listing-12-24/src/main.rs b/listings/ch12-an-io-project/listing-12-24/src/main.rs index fb788d16d9..d6b97defc6 100644 --- a/listings/ch12-an-io-project/listing-12-24/src/main.rs +++ b/listings/ch12-an-io-project/listing-12-24/src/main.rs @@ -8,12 +8,12 @@ fn main() { let args: Vec = env::args().collect(); let config = Config::build(&args).unwrap_or_else(|err| { - eprintln!("Problem parsing arguments: {}", err); + eprintln!("Problem parsing arguments: {err}"); process::exit(1); }); if let Err(e) = minigrep::run(config) { - eprintln!("Application error: {}", e); + eprintln!("Application error: {e}"); process::exit(1); } 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 24fd40e865..ef897e55b2 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 @@ -11,16 +11,16 @@ fn main() { let args: Vec = env::args().collect(); let config = Config::build(&args).unwrap_or_else(|err| { - println!("Problem parsing arguments: {}", err); + println!("Problem parsing arguments: {err}"); process::exit(1); }); // ANCHOR: here println!("Searching for {}", config.query); - println!("In file {}", config.filename); + println!("In file {}", config.file_path); if let Err(e) = run(config) { - println!("Application error: {}", e); + println!("Application error: {e}"); process::exit(1); } @@ -28,16 +28,16 @@ fn main() { // ANCHOR_END: here fn run(config: Config) -> Result<(), Box> { - let contents = fs::read_to_string(config.filename)?; + let contents = fs::read_to_string(config.file_path)?; - println!("With text:\n{}", contents); + println!("With text:\n{contents}"); Ok(()) } struct Config { query: String, - filename: String, + file_path: String, } impl Config { @@ -47,8 +47,8 @@ impl Config { } let query = args[1].clone(); - let filename = args[2].clone(); + let file_path = args[2].clone(); - Ok(Config { query, filename }) + Ok(Config { query, file_path }) } } 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 ca30c2e0f8..e06eae4cda 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 @@ -3,7 +3,7 @@ use std::fs; pub struct Config { pub query: String, - pub filename: String, + pub file_path: String, } impl Config { @@ -13,18 +13,18 @@ impl Config { } let query = args[1].clone(); - let filename = args[2].clone(); + let file_path = args[2].clone(); - Ok(Config { query, filename }) + Ok(Config { query, file_path }) } } // ANCHOR: here pub fn run(config: Config) -> Result<(), Box> { - let contents = fs::read_to_string(config.filename)?; + let contents = fs::read_to_string(config.file_path)?; for line in search(&config.query, &contents) { - println!("{}", line); + println!("{line}"); } Ok(()) diff --git a/listings/ch12-an-io-project/no-listing-02-using-search-in-run/src/main.rs b/listings/ch12-an-io-project/no-listing-02-using-search-in-run/src/main.rs index 5f362421a7..e355a4fb94 100644 --- a/listings/ch12-an-io-project/no-listing-02-using-search-in-run/src/main.rs +++ b/listings/ch12-an-io-project/no-listing-02-using-search-in-run/src/main.rs @@ -7,12 +7,12 @@ fn main() { let args: Vec = env::args().collect(); let config = Config::build(&args).unwrap_or_else(|err| { - println!("Problem parsing arguments: {}", err); + println!("Problem parsing arguments: {err}"); process::exit(1); }); if let Err(e) = minigrep::run(config) { - println!("Application error: {}", e); + println!("Application error: {e}"); process::exit(1); } diff --git a/listings/ch12-an-io-project/output-only-01-with-args/src/main.rs b/listings/ch12-an-io-project/output-only-01-with-args/src/main.rs index aa3056de60..ae7def53d6 100644 --- a/listings/ch12-an-io-project/output-only-01-with-args/src/main.rs +++ b/listings/ch12-an-io-project/output-only-01-with-args/src/main.rs @@ -2,5 +2,5 @@ use std::env; fn main() { let args: Vec = env::args().collect(); - println!("{:?}", args); + dbg!(args); } 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 033fd3eeb3..df623bdeaa 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 @@ -3,7 +3,7 @@ use std::fs; pub struct Config { pub query: String, - pub filename: String, + pub file_path: String, } impl Config { @@ -13,14 +13,14 @@ impl Config { } let query = args[1].clone(); - let filename = args[2].clone(); + let file_path = args[2].clone(); - Ok(Config { query, filename }) + Ok(Config { query, file_path }) } } pub fn run(config: Config) -> Result<(), Box> { - let contents = fs::read_to_string(config.filename)?; + let contents = fs::read_to_string(config.file_path)?; Ok(()) } diff --git a/listings/ch12-an-io-project/output-only-02-missing-lifetimes/src/main.rs b/listings/ch12-an-io-project/output-only-02-missing-lifetimes/src/main.rs index 5f362421a7..e355a4fb94 100644 --- a/listings/ch12-an-io-project/output-only-02-missing-lifetimes/src/main.rs +++ b/listings/ch12-an-io-project/output-only-02-missing-lifetimes/src/main.rs @@ -7,12 +7,12 @@ fn main() { let args: Vec = env::args().collect(); let config = Config::build(&args).unwrap_or_else(|err| { - println!("Problem parsing arguments: {}", err); + println!("Problem parsing arguments: {err}"); process::exit(1); }); if let Err(e) = minigrep::run(config) { - println!("Application error: {}", e); + println!("Application error: {e}"); process::exit(1); } 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 ca30c2e0f8..e06eae4cda 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 @@ -3,7 +3,7 @@ use std::fs; pub struct Config { pub query: String, - pub filename: String, + pub file_path: String, } impl Config { @@ -13,18 +13,18 @@ impl Config { } let query = args[1].clone(); - let filename = args[2].clone(); + let file_path = args[2].clone(); - Ok(Config { query, filename }) + Ok(Config { query, file_path }) } } // ANCHOR: here pub fn run(config: Config) -> Result<(), Box> { - let contents = fs::read_to_string(config.filename)?; + let contents = fs::read_to_string(config.file_path)?; for line in search(&config.query, &contents) { - println!("{}", line); + println!("{line}"); } Ok(()) diff --git a/listings/ch12-an-io-project/output-only-03-multiple-matches/src/main.rs b/listings/ch12-an-io-project/output-only-03-multiple-matches/src/main.rs index 5f362421a7..e355a4fb94 100644 --- a/listings/ch12-an-io-project/output-only-03-multiple-matches/src/main.rs +++ b/listings/ch12-an-io-project/output-only-03-multiple-matches/src/main.rs @@ -7,12 +7,12 @@ fn main() { let args: Vec = env::args().collect(); let config = Config::build(&args).unwrap_or_else(|err| { - println!("Problem parsing arguments: {}", err); + println!("Problem parsing arguments: {err}"); process::exit(1); }); if let Err(e) = minigrep::run(config) { - println!("Application error: {}", e); + println!("Application error: {e}"); process::exit(1); } 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 ca30c2e0f8..e06eae4cda 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 @@ -3,7 +3,7 @@ use std::fs; pub struct Config { pub query: String, - pub filename: String, + pub file_path: String, } impl Config { @@ -13,18 +13,18 @@ impl Config { } let query = args[1].clone(); - let filename = args[2].clone(); + let file_path = args[2].clone(); - Ok(Config { query, filename }) + Ok(Config { query, file_path }) } } // ANCHOR: here pub fn run(config: Config) -> Result<(), Box> { - let contents = fs::read_to_string(config.filename)?; + let contents = fs::read_to_string(config.file_path)?; for line in search(&config.query, &contents) { - println!("{}", line); + println!("{line}"); } Ok(()) diff --git a/listings/ch12-an-io-project/output-only-04-no-matches/src/main.rs b/listings/ch12-an-io-project/output-only-04-no-matches/src/main.rs index 5f362421a7..e355a4fb94 100644 --- a/listings/ch12-an-io-project/output-only-04-no-matches/src/main.rs +++ b/listings/ch12-an-io-project/output-only-04-no-matches/src/main.rs @@ -7,12 +7,12 @@ fn main() { let args: Vec = env::args().collect(); let config = Config::build(&args).unwrap_or_else(|err| { - println!("Problem parsing arguments: {}", err); + println!("Problem parsing arguments: {err}"); process::exit(1); }); if let Err(e) = minigrep::run(config) { - println!("Application error: {}", e); + println!("Application error: {e}"); process::exit(1); } 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 df3bec5ceb..e54343d242 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 @@ -4,7 +4,7 @@ use std::fs; pub struct Config { pub query: String, - pub filename: String, + pub file_path: String, pub ignore_case: bool, } @@ -16,13 +16,13 @@ impl Config { } let query = args[1].clone(); - let filename = args[2].clone(); + let file_path = args[2].clone(); let ignore_case = env::var("IGNORE_CASE").is_ok(); Ok(Config { query, - filename, + file_path, ignore_case, }) } @@ -30,7 +30,7 @@ impl Config { // ANCHOR_END: ch13 pub fn run(config: Config) -> Result<(), Box> { - let contents = fs::read_to_string(config.filename)?; + let contents = fs::read_to_string(config.file_path)?; let results = if config.ignore_case { search_case_insensitive(&config.query, &contents) @@ -39,7 +39,7 @@ pub fn run(config: Config) -> Result<(), Box> { }; for line in results { - println!("{}", line); + println!("{line}"); } Ok(()) diff --git a/listings/ch13-functional-features/listing-12-23-reproduced/src/main.rs b/listings/ch13-functional-features/listing-12-23-reproduced/src/main.rs index 5f362421a7..e355a4fb94 100644 --- a/listings/ch13-functional-features/listing-12-23-reproduced/src/main.rs +++ b/listings/ch13-functional-features/listing-12-23-reproduced/src/main.rs @@ -7,12 +7,12 @@ fn main() { let args: Vec = env::args().collect(); let config = Config::build(&args).unwrap_or_else(|err| { - println!("Problem parsing arguments: {}", err); + println!("Problem parsing arguments: {err}"); process::exit(1); }); if let Err(e) = minigrep::run(config) { - println!("Application error: {}", e); + println!("Application error: {e}"); process::exit(1); } 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 511b0136f2..292b097897 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 @@ -4,7 +4,7 @@ use std::fs; pub struct Config { pub query: String, - pub filename: String, + pub file_path: String, pub ignore_case: bool, } @@ -15,20 +15,20 @@ impl Config { } let query = args[1].clone(); - let filename = args[2].clone(); + let file_path = args[2].clone(); let ignore_case = env::var("IGNORE_CASE").is_ok(); Ok(Config { query, - filename, + file_path, ignore_case, }) } } pub fn run(config: Config) -> Result<(), Box> { - let contents = fs::read_to_string(config.filename)?; + let contents = fs::read_to_string(config.file_path)?; let results = if config.ignore_case { search_case_insensitive(&config.query, &contents) @@ -37,7 +37,7 @@ pub fn run(config: Config) -> Result<(), Box> { }; for line in results { - println!("{}", line); + println!("{line}"); } Ok(()) diff --git a/listings/ch13-functional-features/listing-12-24-reproduced/src/main.rs b/listings/ch13-functional-features/listing-12-24-reproduced/src/main.rs index 21603de1ed..c1a0d74311 100644 --- a/listings/ch13-functional-features/listing-12-24-reproduced/src/main.rs +++ b/listings/ch13-functional-features/listing-12-24-reproduced/src/main.rs @@ -8,7 +8,7 @@ fn main() { let args: Vec = env::args().collect(); let config = Config::build(&args).unwrap_or_else(|err| { - eprintln!("Problem parsing arguments: {}", err); + eprintln!("Problem parsing arguments: {err}"); process::exit(1); }); @@ -16,7 +16,7 @@ fn main() { // ANCHOR_END: ch13 if let Err(e) = minigrep::run(config) { - eprintln!("Application error: {}", e); + eprintln!("Application error: {e}"); process::exit(1); } diff --git a/listings/ch13-functional-features/listing-13-18/src/lib.rs b/listings/ch13-functional-features/listing-13-18/src/lib.rs index 511b0136f2..292b097897 100644 --- a/listings/ch13-functional-features/listing-13-18/src/lib.rs +++ b/listings/ch13-functional-features/listing-13-18/src/lib.rs @@ -4,7 +4,7 @@ use std::fs; pub struct Config { pub query: String, - pub filename: String, + pub file_path: String, pub ignore_case: bool, } @@ -15,20 +15,20 @@ impl Config { } let query = args[1].clone(); - let filename = args[2].clone(); + let file_path = args[2].clone(); let ignore_case = env::var("IGNORE_CASE").is_ok(); Ok(Config { query, - filename, + file_path, ignore_case, }) } } pub fn run(config: Config) -> Result<(), Box> { - let contents = fs::read_to_string(config.filename)?; + let contents = fs::read_to_string(config.file_path)?; let results = if config.ignore_case { search_case_insensitive(&config.query, &contents) @@ -37,7 +37,7 @@ pub fn run(config: Config) -> Result<(), Box> { }; for line in results { - println!("{}", line); + println!("{line}"); } Ok(()) diff --git a/listings/ch13-functional-features/listing-13-18/src/main.rs b/listings/ch13-functional-features/listing-13-18/src/main.rs index 62ceb86442..64e0f112db 100644 --- a/listings/ch13-functional-features/listing-13-18/src/main.rs +++ b/listings/ch13-functional-features/listing-13-18/src/main.rs @@ -6,7 +6,7 @@ use minigrep::Config; // ANCHOR: here fn main() { let config = Config::build(env::args()).unwrap_or_else(|err| { - eprintln!("Problem parsing arguments: {}", err); + eprintln!("Problem parsing arguments: {err}"); process::exit(1); }); @@ -14,7 +14,7 @@ fn main() { // ANCHOR_END: here if let Err(e) = minigrep::run(config) { - eprintln!("Application error: {}", e); + eprintln!("Application error: {e}"); process::exit(1); } 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 3a991f7079..79ae2b8f6c 100644 --- a/listings/ch13-functional-features/listing-13-19/src/lib.rs +++ b/listings/ch13-functional-features/listing-13-19/src/lib.rs @@ -4,7 +4,7 @@ use std::fs; pub struct Config { pub query: String, - pub filename: String, + pub file_path: String, pub ignore_case: bool, } @@ -20,20 +20,20 @@ impl Config { } let query = args[1].clone(); - let filename = args[2].clone(); + let file_path = args[2].clone(); let ignore_case = env::var("IGNORE_CASE").is_ok(); Ok(Config { query, - filename, + file_path, ignore_case, }) } } pub fn run(config: Config) -> Result<(), Box> { - let contents = fs::read_to_string(config.filename)?; + let contents = fs::read_to_string(config.file_path)?; let results = if config.ignore_case { search_case_insensitive(&config.query, &contents) @@ -42,7 +42,7 @@ pub fn run(config: Config) -> Result<(), Box> { }; for line in results { - println!("{}", line); + println!("{line}"); } Ok(()) diff --git a/listings/ch13-functional-features/listing-13-19/src/main.rs b/listings/ch13-functional-features/listing-13-19/src/main.rs index 843f720b65..a8877627d4 100644 --- a/listings/ch13-functional-features/listing-13-19/src/main.rs +++ b/listings/ch13-functional-features/listing-13-19/src/main.rs @@ -5,12 +5,12 @@ use minigrep::Config; fn main() { let config = Config::build(env::args()).unwrap_or_else(|err| { - eprintln!("Problem parsing arguments: {}", err); + eprintln!("Problem parsing arguments: {err}"); process::exit(1); }); if let Err(e) = minigrep::run(config) { - eprintln!("Application error: {}", e); + eprintln!("Application error: {e}"); process::exit(1); } diff --git a/listings/ch13-functional-features/listing-13-20/src/lib.rs b/listings/ch13-functional-features/listing-13-20/src/lib.rs index 02f4c89a4c..4410964a7d 100644 --- a/listings/ch13-functional-features/listing-13-20/src/lib.rs +++ b/listings/ch13-functional-features/listing-13-20/src/lib.rs @@ -4,7 +4,7 @@ use std::fs; pub struct Config { pub query: String, - pub filename: String, + pub file_path: String, pub ignore_case: bool, } @@ -20,16 +20,16 @@ impl Config { None => return Err("Didn't get a query string"), }; - let filename = match args.next() { + let file_path = match args.next() { Some(arg) => arg, - None => return Err("Didn't get a file name"), + None => return Err("Didn't get a file path"), }; let ignore_case = env::var("IGNORE_CASE").is_ok(); Ok(Config { query, - filename, + file_path, ignore_case, }) } @@ -37,7 +37,7 @@ impl Config { // ANCHOR_END: here pub fn run(config: Config) -> Result<(), Box> { - let contents = fs::read_to_string(config.filename)?; + let contents = fs::read_to_string(config.file_path)?; let results = if config.ignore_case { search_case_insensitive(&config.query, &contents) @@ -46,7 +46,7 @@ pub fn run(config: Config) -> Result<(), Box> { }; for line in results { - println!("{}", line); + println!("{line}"); } Ok(()) diff --git a/listings/ch13-functional-features/listing-13-20/src/main.rs b/listings/ch13-functional-features/listing-13-20/src/main.rs index 843f720b65..a8877627d4 100644 --- a/listings/ch13-functional-features/listing-13-20/src/main.rs +++ b/listings/ch13-functional-features/listing-13-20/src/main.rs @@ -5,12 +5,12 @@ use minigrep::Config; fn main() { let config = Config::build(env::args()).unwrap_or_else(|err| { - eprintln!("Problem parsing arguments: {}", err); + eprintln!("Problem parsing arguments: {err}"); process::exit(1); }); if let Err(e) = minigrep::run(config) { - eprintln!("Application error: {}", e); + eprintln!("Application error: {e}"); process::exit(1); } diff --git a/listings/ch13-functional-features/listing-13-22/src/lib.rs b/listings/ch13-functional-features/listing-13-22/src/lib.rs index 7922a5a357..d694669b45 100644 --- a/listings/ch13-functional-features/listing-13-22/src/lib.rs +++ b/listings/ch13-functional-features/listing-13-22/src/lib.rs @@ -4,7 +4,7 @@ use std::fs; pub struct Config { pub query: String, - pub filename: String, + pub file_path: String, pub ignore_case: bool, } @@ -19,23 +19,23 @@ impl Config { None => return Err("Didn't get a query string"), }; - let filename = match args.next() { + let file_path = match args.next() { Some(arg) => arg, - None => return Err("Didn't get a file name"), + None => return Err("Didn't get a file path"), }; let ignore_case = env::var("IGNORE_CASE").is_ok(); Ok(Config { query, - filename, + file_path, ignore_case, }) } } pub fn run(config: Config) -> Result<(), Box> { - let contents = fs::read_to_string(config.filename)?; + let contents = fs::read_to_string(config.file_path)?; let results = if config.ignore_case { search_case_insensitive(&config.query, &contents) @@ -44,7 +44,7 @@ pub fn run(config: Config) -> Result<(), Box> { }; for line in results { - println!("{}", line); + println!("{line}"); } Ok(()) diff --git a/listings/ch13-functional-features/listing-13-22/src/main.rs b/listings/ch13-functional-features/listing-13-22/src/main.rs index 843f720b65..a8877627d4 100644 --- a/listings/ch13-functional-features/listing-13-22/src/main.rs +++ b/listings/ch13-functional-features/listing-13-22/src/main.rs @@ -5,12 +5,12 @@ use minigrep::Config; fn main() { let config = Config::build(env::args()).unwrap_or_else(|err| { - eprintln!("Problem parsing arguments: {}", err); + eprintln!("Problem parsing arguments: {err}"); process::exit(1); }); if let Err(e) = minigrep::run(config) { - eprintln!("Application error: {}", e); + eprintln!("Application error: {e}"); process::exit(1); } diff --git a/nostarch/chapter12.md b/nostarch/chapter12.md index 3d1f6dfbb0..f2b2825095 100644 --- a/nostarch/chapter12.md +++ b/nostarch/chapter12.md @@ -18,8 +18,8 @@ an ideal language for creating command line tools, so for our project, we’ll make our own version of the classic command line search tool `grep` (**g**lobally search a **r**egular **e**xpression and **p**rint). In the simplest use case, `grep` searches a specified file for a specified string. To -do so, `grep` takes as its arguments a filename and a string. Then it reads the -file, finds lines in that file that contain the string argument, and prints +do so, `grep` takes as its arguments a file path and a string. Then it reads +the file, finds lines in that file that contain the string argument, and prints those lines. Along the way, we’ll show how to make our command line tool use the terminal @@ -59,7 +59,7 @@ $ cd minigrep ``` The first task is to make `minigrep` accept its two command line arguments: the -filename and a string to search for. That is, we want to be able to run our +file path and a string to search for. That is, we want to be able to run our program with `cargo run`, two hyphens to indicate the following arguments are for our program rather than for `cargo`, a string to search for, and a path to a file to search in, like so: @@ -115,7 +115,7 @@ use std::env; fn main() { let args: Vec = env::args().collect(); - println!("{:?}", args); + dbg!(args); } ``` @@ -164,8 +164,8 @@ want a vector of strings. Although we very rarely need to annotate types in Rust, `collect` is one function you do often need to annotate because Rust isn’t able to infer the kind of collection you want. -Finally, we print the vector using the debug formatter, `:?`. Let’s try running -the code first with no arguments and then with two arguments: +Finally, we print the vector using the debug macro. Let’s try running the code +first with no arguments and then with two arguments: ``` $ cargo run @@ -203,22 +203,22 @@ fn main() { let args: Vec = env::args().collect(); let query = &args[1]; - let filename = &args[2]; + let file_path = &args[2]; println!("Searching for {}", query); - println!("In file {}", filename); + println!("In file {}", file_path); } ``` -Listing 12-2: Creating variables to hold the query argument and filename +Listing 12-2: Creating variables to hold the query argument and file path argument As we saw when we printed the vector, the program’s name takes up the first value in the vector at `args[0]`, so we’re starting arguments at index `1`. The first argument `minigrep` takes is the string we’re searching for, so we put a reference to the first argument in the variable `query`. The second argument -will be the filename, so we put a reference to the second argument in the -variable `filename`. +will be the file path, so we put a reference to the second argument in the +variable `file_path`. We temporarily print the values of these variables to prove that the code is working as we intend. Let’s run this program again with the arguments `test` @@ -241,7 +241,7 @@ capabilities instead. ## Reading a File -Now we’ll add functionality to read the file specified in the `filename` +Now we’ll add functionality to read the file specified in the `file_path` argument. First, we need a sample file to test it with: we’ll use a file with a small amount of text over multiple lines with some repeated words. Listing 12-3 has an Emily Dickinson poem that will work well! Create a file called @@ -275,12 +275,12 @@ use std::env; fn main() { // --snip-- - println!("In file {}", filename); + println!("In file {}", file_path); - [2] let contents = fs::read_to_string(filename) - .expect("Something went wrong reading the file"); + [2] let contents = fs::read_to_string(file_path) + .expect("Should have been able to read the file"); - [3] println!("With text:\n{}", contents); + [3] println!("With text:\n{contents}"); } ``` @@ -289,7 +289,7 @@ Listing 12-4: Reading the contents of the file specified by the second argument First, we bring in a relevant part of the standard library with a `use` statement: we need `std::fs` to handle files [1]. -In `main`, the new statement `fs::read_to_string` takes the `filename`, opens +In `main`, the new statement `fs::read_to_string` takes the `file_path`, opens that file, and returns a `std::io::Result` of the file’s contents [2]. , you’ll learn how to use more efficient > methods in this type of situation. But for now, it’s okay to copy a few > strings to continue making progress because you’ll make these copies only -> once and your filename and query string are very small. It’s better to have +> once and your file path and query string are very small. It’s better to have > a working program that’s a bit inefficient than to try to hyperoptimize code > on your first pass. As you become more experienced with Rust, it’ll be > easier to start with the most efficient solution, but for now, it’s @@ -153,10 +153,10 @@ giving up a little performance to gain simplicity is a worthwhile trade-off. We’ve updated `main` so it places the instance of `Config` returned by `parse_config` into a variable named `config`, and we updated the code that -previously used the separate `query` and `filename` variables so it now uses +previously used the separate `query` and `file_path` variables so it now uses the fields on the `Config` struct instead. -Now our code more clearly conveys that `query` and `filename` are related and +Now our code more clearly conveys that `query` and `file_path` are related and that their purpose is to configure how the program will work. Any code that uses these values knows to find them in the `config` instance in the fields named for their purpose. @@ -165,9 +165,9 @@ named for their purpose. So far, we’ve extracted the logic responsible for parsing the command line arguments from `main` and placed it in the `parse_config` function. Doing so -helped us to see that the `query` and `filename` values were related and that +helped us to see that the `query` and `file_path` values were related and that relationship should be conveyed in our code. We then added a `Config` struct to -name the related purpose of `query` and `filename` and to be able to return the +name the related purpose of `query` and `file_path` and to be able to return the values’ names as struct field names from the `parse_config` function. So now that the purpose of the `parse_config` function is to create a `Config` diff --git a/src/ch12-06-writing-to-stderr-instead-of-stdout.md b/src/ch12-06-writing-to-stderr-instead-of-stdout.md index 5251766533..d017fa3245 100644 --- a/src/ch12-06-writing-to-stderr-instead-of-stdout.md +++ b/src/ch12-06-writing-to-stderr-instead-of-stdout.md @@ -24,7 +24,7 @@ stream so we can still see error messages on the screen even if we redirect the standard output stream to a file. Our program is not currently well-behaved: we’re about to see that it saves the error message output to a file instead! -To demonstrate this behavior, we’ll run the program with `>` and the filename, +To demonstrate this behavior, we’ll run the program with `>` and the file path, *output.txt*, that we want to redirect the standard output stream to. We won’t pass any arguments, which should cause an error: