Skip to content

Commit 02a168e

Browse files
committed
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.
1 parent 28eb8ba commit 02a168e

File tree

64 files changed

+316
-316
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+316
-316
lines changed

listings/ch12-an-io-project/listing-12-01/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ use std::env;
22

33
fn main() {
44
let args: Vec<String> = env::args().collect();
5-
println!("{:?}", args);
5+
dbg!(args);
66
}

listings/ch12-an-io-project/listing-12-02/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ fn main() {
44
let args: Vec<String> = env::args().collect();
55

66
let query = &args[1];
7-
let filename = &args[2];
7+
let file_path = &args[2];
88

99
println!("Searching for {}", query);
10-
println!("In file {}", filename);
10+
println!("In file {}", file_path);
1111
}

listings/ch12-an-io-project/listing-12-03/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ fn main() {
44
let args: Vec<String> = env::args().collect();
55

66
let query = &args[1];
7-
let filename = &args[2];
7+
let file_path = &args[2];
88

99
println!("Searching for {}", query);
10-
println!("In file {}", filename);
10+
println!("In file {}", file_path);
1111
}

listings/ch12-an-io-project/listing-12-04/src/main.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ fn main() {
88
let args: Vec<String> = env::args().collect();
99

1010
let query = &args[1];
11-
let filename = &args[2];
11+
let file_path = &args[2];
1212

1313
println!("Searching for {}", query);
1414
// ANCHOR: here
15-
println!("In file {}", filename);
15+
println!("In file {}", file_path);
1616

17-
let contents = fs::read_to_string(filename)
18-
.expect("Something went wrong reading the file");
17+
let contents = fs::read_to_string(file_path)
18+
.expect("Should have been able to read the file");
1919

20-
println!("With text:\n{}", contents);
20+
println!("With text:\n{contents}");
2121
}
2222
// ANCHOR_END: here

listings/ch12-an-io-project/listing-12-05/src/main.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@ use std::fs;
55
fn main() {
66
let args: Vec<String> = env::args().collect();
77

8-
let (query, filename) = parse_config(&args);
8+
let (query, file_path) = parse_config(&args);
99

1010
// --snip--
1111
// ANCHOR_END: here
1212

1313
println!("Searching for {}", query);
14-
println!("In file {}", filename);
14+
println!("In file {}", file_path);
1515

16-
let contents = fs::read_to_string(filename)
17-
.expect("Something went wrong reading the file");
16+
let contents = fs::read_to_string(file_path)
17+
.expect("Should have been able to read the file");
1818

19-
println!("With text:\n{}", contents);
19+
println!("With text:\n{contents}");
2020
// ANCHOR: here
2121
}
2222

2323
fn parse_config(args: &[String]) -> (&str, &str) {
2424
let query = &args[1];
25-
let filename = &args[2];
25+
let file_path = &args[2];
2626

27-
(query, filename)
27+
(query, file_path)
2828
}
2929
// ANCHOR_END: here

listings/ch12-an-io-project/listing-12-06/src/main.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,27 @@ fn main() {
88
let config = parse_config(&args);
99

1010
println!("Searching for {}", config.query);
11-
println!("In file {}", config.filename);
11+
println!("In file {}", config.file_path);
1212

13-
let contents = fs::read_to_string(config.filename)
14-
.expect("Something went wrong reading the file");
13+
let contents = fs::read_to_string(config.file_path)
14+
.expect("Should have been able to read the file");
1515

1616
// --snip--
1717
// ANCHOR_END: here
1818

19-
println!("With text:\n{}", contents);
19+
println!("With text:\n{contents}");
2020
// ANCHOR: here
2121
}
2222

2323
struct Config {
2424
query: String,
25-
filename: String,
25+
file_path: String,
2626
}
2727

2828
fn parse_config(args: &[String]) -> Config {
2929
let query = args[1].clone();
30-
let filename = args[2].clone();
30+
let file_path = args[2].clone();
3131

32-
Config { query, filename }
32+
Config { query, file_path }
3333
}
3434
// ANCHOR_END: here

listings/ch12-an-io-project/listing-12-07/src/main.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ fn main() {
99
// ANCHOR_END: here
1010

1111
println!("Searching for {}", config.query);
12-
println!("In file {}", config.filename);
12+
println!("In file {}", config.file_path);
1313

14-
let contents = fs::read_to_string(config.filename)
15-
.expect("Something went wrong reading the file");
14+
let contents = fs::read_to_string(config.file_path)
15+
.expect("Should have been able to read the file");
1616

17-
println!("With text:\n{}", contents);
17+
println!("With text:\n{contents}");
1818
// ANCHOR: here
1919

2020
// --snip--
@@ -25,16 +25,16 @@ fn main() {
2525
// ANCHOR_END: here
2626
struct Config {
2727
query: String,
28-
filename: String,
28+
file_path: String,
2929
}
3030

3131
// ANCHOR: here
3232
impl Config {
3333
fn new(args: &[String]) -> Config {
3434
let query = args[1].clone();
35-
let filename = args[2].clone();
35+
let file_path = args[2].clone();
3636

37-
Config { query, filename }
37+
Config { query, file_path }
3838
}
3939
}
4040
// ANCHOR_END: here

listings/ch12-an-io-project/listing-12-08/src/main.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ fn main() {
77
let config = Config::new(&args);
88

99
println!("Searching for {}", config.query);
10-
println!("In file {}", config.filename);
10+
println!("In file {}", config.file_path);
1111

12-
let contents = fs::read_to_string(config.filename)
13-
.expect("Something went wrong reading the file");
12+
let contents = fs::read_to_string(config.file_path)
13+
.expect("Should have been able to read the file");
1414

15-
println!("With text:\n{}", contents);
15+
println!("With text:\n{contents}");
1616
}
1717

1818
struct Config {
1919
query: String,
20-
filename: String,
20+
file_path: String,
2121
}
2222

2323
impl Config {
@@ -31,8 +31,8 @@ impl Config {
3131
// ANCHOR_END: here
3232

3333
let query = args[1].clone();
34-
let filename = args[2].clone();
34+
let file_path = args[2].clone();
3535

36-
Config { query, filename }
36+
Config { query, file_path }
3737
}
3838
}

listings/ch12-an-io-project/listing-12-09/src/main.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ fn main() {
77
let config = Config::new(&args);
88

99
println!("Searching for {}", config.query);
10-
println!("In file {}", config.filename);
10+
println!("In file {}", config.file_path);
1111

12-
let contents = fs::read_to_string(config.filename)
13-
.expect("Something went wrong reading the file");
12+
let contents = fs::read_to_string(config.file_path)
13+
.expect("Should have been able to read the file");
1414

15-
println!("With text:\n{}", contents);
15+
println!("With text:\n{contents}");
1616
}
1717

1818
struct Config {
1919
query: String,
20-
filename: String,
20+
file_path: String,
2121
}
2222

2323
// ANCHOR: here
@@ -28,9 +28,9 @@ impl Config {
2828
}
2929

3030
let query = args[1].clone();
31-
let filename = args[2].clone();
31+
let file_path = args[2].clone();
3232

33-
Ok(Config { query, filename })
33+
Ok(Config { query, file_path })
3434
}
3535
}
3636
// ANCHOR_END: here

listings/ch12-an-io-project/listing-12-10/src/main.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,25 @@ fn main() {
77
let args: Vec<String> = env::args().collect();
88

99
let config = Config::build(&args).unwrap_or_else(|err| {
10-
println!("Problem parsing arguments: {}", err);
10+
println!("Problem parsing arguments: {err}");
1111
process::exit(1);
1212
});
1313

1414
// --snip--
1515
// ANCHOR_END: here
1616

1717
println!("Searching for {}", config.query);
18-
println!("In file {}", config.filename);
18+
println!("In file {}", config.file_path);
1919

20-
let contents = fs::read_to_string(config.filename)
21-
.expect("Something went wrong reading the file");
20+
let contents = fs::read_to_string(config.file_path)
21+
.expect("Should have been able to read the file");
2222

23-
println!("With text:\n{}", contents);
23+
println!("With text:\n{contents}");
2424
}
2525

2626
struct Config {
2727
query: String,
28-
filename: String,
28+
file_path: String,
2929
}
3030

3131
impl Config {
@@ -35,8 +35,8 @@ impl Config {
3535
}
3636

3737
let query = args[1].clone();
38-
let filename = args[2].clone();
38+
let file_path = args[2].clone();
3939

40-
Ok(Config { query, filename })
40+
Ok(Config { query, file_path })
4141
}
4242
}

listings/ch12-an-io-project/listing-12-11/src/main.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ fn main() {
1010
let args: Vec<String> = env::args().collect();
1111

1212
let config = Config::build(&args).unwrap_or_else(|err| {
13-
println!("Problem parsing arguments: {}", err);
13+
println!("Problem parsing arguments: {err}");
1414
process::exit(1);
1515
});
1616

1717
// ANCHOR: here
1818
println!("Searching for {}", config.query);
19-
println!("In file {}", config.filename);
19+
println!("In file {}", config.file_path);
2020

2121
run(config);
2222
}
2323

2424
fn run(config: Config) {
25-
let contents = fs::read_to_string(config.filename)
26-
.expect("Something went wrong reading the file");
25+
let contents = fs::read_to_string(config.file_path)
26+
.expect("Should have been able to read the file");
2727

28-
println!("With text:\n{}", contents);
28+
println!("With text:\n{contents}");
2929
}
3030

3131
// --snip--
3232
// ANCHOR_END: here
3333

3434
struct Config {
3535
query: String,
36-
filename: String,
36+
file_path: String,
3737
}
3838

3939
impl Config {
@@ -43,8 +43,8 @@ impl Config {
4343
}
4444

4545
let query = args[1].clone();
46-
let filename = args[2].clone();
46+
let file_path = args[2].clone();
4747

48-
Ok(Config { query, filename })
48+
Ok(Config { query, file_path })
4949
}
5050
}

listings/ch12-an-io-project/listing-12-12/src/main.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,29 @@ fn main() {
1212
let args: Vec<String> = env::args().collect();
1313

1414
let config = Config::build(&args).unwrap_or_else(|err| {
15-
println!("Problem parsing arguments: {}", err);
15+
println!("Problem parsing arguments: {err}");
1616
process::exit(1);
1717
});
1818

1919
println!("Searching for {}", config.query);
20-
println!("In file {}", config.filename);
20+
println!("In file {}", config.file_path);
2121

2222
run(config);
2323
}
2424

2525
// ANCHOR: here
2626
fn run(config: Config) -> Result<(), Box<dyn Error>> {
27-
let contents = fs::read_to_string(config.filename)?;
27+
let contents = fs::read_to_string(config.file_path)?;
2828

29-
println!("With text:\n{}", contents);
29+
println!("With text:\n{contents}");
3030

3131
Ok(())
3232
}
3333
// ANCHOR_END: here
3434

3535
struct Config {
3636
query: String,
37-
filename: String,
37+
file_path: String,
3838
}
3939

4040
impl Config {
@@ -44,8 +44,8 @@ impl Config {
4444
}
4545

4646
let query = args[1].clone();
47-
let filename = args[2].clone();
47+
let file_path = args[2].clone();
4848

49-
Ok(Config { query, filename })
49+
Ok(Config { query, file_path })
5050
}
5151
}

listings/ch12-an-io-project/listing-12-13/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::fs;
44

55
pub struct Config {
66
pub query: String,
7-
pub filename: String,
7+
pub file_path: String,
88
}
99

1010
impl Config {
@@ -16,19 +16,19 @@ impl Config {
1616
}
1717

1818
let query = args[1].clone();
19-
let filename = args[2].clone();
19+
let file_path = args[2].clone();
2020

21-
Ok(Config { query, filename })
21+
Ok(Config { query, file_path })
2222
// ANCHOR: here
2323
}
2424
}
2525

2626
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
2727
// --snip--
2828
// ANCHOR_END: here
29-
let contents = fs::read_to_string(config.filename)?;
29+
let contents = fs::read_to_string(config.file_path)?;
3030

31-
println!("With text:\n{}", contents);
31+
println!("With text:\n{contents}");
3232

3333
Ok(())
3434
// ANCHOR: here

0 commit comments

Comments
 (0)