Skip to content

Commit

Permalink
More little improvements to chapter 12
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
carols10cents committed Jun 4, 2022
1 parent d2c3027 commit a9fb154
Show file tree
Hide file tree
Showing 64 changed files with 316 additions and 316 deletions.
2 changes: 1 addition & 1 deletion listings/ch12-an-io-project/listing-12-01/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ use std::env;

fn main() {
let args: Vec<String> = env::args().collect();
println!("{:?}", args);
dbg!(args);
}
4 changes: 2 additions & 2 deletions listings/ch12-an-io-project/listing-12-02/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ fn main() {
let args: Vec<String> = 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);
}
4 changes: 2 additions & 2 deletions listings/ch12-an-io-project/listing-12-03/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ fn main() {
let args: Vec<String> = 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);
}
10 changes: 5 additions & 5 deletions listings/ch12-an-io-project/listing-12-04/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ fn main() {
let args: Vec<String> = 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
14 changes: 7 additions & 7 deletions listings/ch12-an-io-project/listing-12-05/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@ use std::fs;
fn main() {
let args: Vec<String> = 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
14 changes: 7 additions & 7 deletions listings/ch12-an-io-project/listing-12-06/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 7 additions & 7 deletions listings/ch12-an-io-project/listing-12-07/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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--
Expand All @@ -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
14 changes: 7 additions & 7 deletions listings/ch12-an-io-project/listing-12-08/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 }
}
}
14 changes: 7 additions & 7 deletions listings/ch12-an-io-project/listing-12-09/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
16 changes: 8 additions & 8 deletions listings/ch12-an-io-project/listing-12-10/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@ fn main() {
let args: Vec<String> = 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);
});

// --snip--
// 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 {
Expand All @@ -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 })
}
}
16 changes: 8 additions & 8 deletions listings/ch12-an-io-project/listing-12-11/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ fn main() {
let args: Vec<String> = 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--
// ANCHOR_END: here

struct Config {
query: String,
filename: String,
file_path: String,
}

impl Config {
Expand All @@ -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 })
}
}
14 changes: 7 additions & 7 deletions listings/ch12-an-io-project/listing-12-12/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,29 @@ fn main() {
let args: Vec<String> = 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<dyn Error>> {
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_END: here

struct Config {
query: String,
filename: String,
file_path: String,
}

impl Config {
Expand All @@ -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 })
}
}
10 changes: 5 additions & 5 deletions listings/ch12-an-io-project/listing-12-13/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::fs;

pub struct Config {
pub query: String,
pub filename: String,
pub file_path: String,
}

impl Config {
Expand All @@ -16,19 +16,19 @@ 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<dyn Error>> {
// --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
Expand Down
Loading

0 comments on commit a9fb154

Please sign in to comment.