Skip to content

Commit e7d217b

Browse files
committed
Snapshot changes to ch 12 to consider sending to nostarch
1 parent 084b0e3 commit e7d217b

1 file changed

Lines changed: 47 additions & 40 deletions

File tree

nostarch/chapter12.md

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ $ cargo run
137137
Compiling minigrep v0.1.0 (file:///projects/minigrep)
138138
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.61s
139139
Running `target/debug/minigrep`
140-
[src/main.rs:5] args = [
140+
[src/main.rs:5:5] args = [
141141
"target/debug/minigrep",
142142
]
143143
```
@@ -147,7 +147,7 @@ $ cargo run -- needle haystack
147147
Compiling minigrep v0.1.0 (file:///projects/minigrep)
148148
Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.57s
149149
Running `target/debug/minigrep needle haystack`
150-
[src/main.rs:5] args = [
150+
[src/main.rs:5:5] args = [
151151
"target/debug/minigrep",
152152
"needle",
153153
"haystack",
@@ -181,8 +181,8 @@ fn main() {
181181
let query = &args[1];
182182
let file_path = &args[2];
183183
184-
println!("Searching for {}", query);
185-
println!("In file {}", file_path);
184+
println!("Searching for {query}");
185+
println!("In file {file_path}");
186186
}
187187
```
188188

@@ -202,7 +202,7 @@ and `sample.txt`:
202202
```
203203
$ cargo run -- test sample.txt
204204
Compiling minigrep v0.1.0 (file:///projects/minigrep)
205-
Finished dev [unoptimized + debuginfo] target(s) in 0.0s
205+
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.0s
206206
Running `target/debug/minigrep test sample.txt`
207207
Searching for test
208208
In file sample.txt
@@ -252,7 +252,7 @@ use std::fs;
252252
253253
fn main() {
254254
// --snip--
255-
println!("In file {}", file_path);
255+
println!("In file {file_path}");
256256
257257
let contents = fs::read_to_string(file_path)
258258
.expect("Should have been able to read the file");
@@ -267,7 +267,8 @@ First we bring in a relevant part of the standard library with a `use`
267267
statement: we need `std::fs` to handle files.
268268

269269
In `main`, the new statement `fs::read_to_string` takes the `file_path`, opens
270-
that file, and returns an `std::io::Result<String>` of the file’s contents [2].
270+
that file, and returns a value of type `std::io::Result<String>` that contains
271+
the file’s contents.
271272

272273
After that, we again add a temporary `println!` statement that prints the value
273274
of `contents` after the file is read, so we can check that the program is
@@ -280,7 +281,7 @@ second argument:
280281
```
281282
$ cargo run -- the poem.txt
282283
Compiling minigrep v0.1.0 (file:///projects/minigrep)
283-
Finished dev [unoptimized + debuginfo] target(s) in 0.0s
284+
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.0s
284285
Running `target/debug/minigrep the poem.txt`
285286
Searching for the
286287
In file poem.txt
@@ -331,13 +332,13 @@ example, the file could be missing, or we might not have permission to open it.
331332
Right now, regardless of the situation, we’d print the same error message for
332333
everything, which wouldn’t give the user any information!
333334

334-
Fourth, we use `expect` repeatedly to handle different errors, and if the user
335-
runs our program without specifying enough arguments, they’ll get an `index out
336-
of bounds` error from Rust that doesn’t clearly explain the problem. It would
337-
be best if all the error-handling code were in one place so future maintainers
338-
had only one place to consult the code if the error-handling logic needed to
339-
change. Having all the error-handling code in one place will also ensure that
340-
we’re printing messages that will be meaningful to our end users.
335+
Fourth, we use `expect` to handle an error, and if the user runs our program
336+
without specifying enough arguments, they’ll get an `index out of bounds` error
337+
from Rust that doesn’t clearly explain the problem. It would be best if all the
338+
error-handling code were in one place so future maintainers had only one place
339+
to consult the code if the error-handling logic needed to change. Having all the
340+
error-handling code in one place will also ensure that we’re printing messages
341+
that will be meaningful to our end users.
341342

342343
Let’s address these four problems by refactoring our project.
343344

@@ -566,12 +567,11 @@ without any arguments; it will look like this:
566567
```
567568
$ cargo run
568569
Compiling minigrep v0.1.0 (file:///projects/minigrep)
569-
Finished dev [unoptimized + debuginfo] target(s) in 0.0s
570+
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.0s
570571
Running `target/debug/minigrep`
571-
thread 'main' panicked at 'index out of bounds: the len is 1 but
572-
the index is 1', src/main.rs:27:21
573-
note: run with `RUST_BACKTRACE=1` environment variable to display
574-
a backtrace
572+
thread 'main' panicked at src/main.rs:27:21:
573+
index out of bounds: the len is 1 but the index is 1
574+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
575575
```
576576

577577
The line `index out of bounds: the len is 1 but the index is 1` is an error
@@ -612,12 +612,11 @@ arguments again to see what the error looks like now:
612612
```
613613
$ cargo run
614614
Compiling minigrep v0.1.0 (file:///projects/minigrep)
615-
Finished dev [unoptimized + debuginfo] target(s) in 0.0s
615+
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.0s
616616
Running `target/debug/minigrep`
617-
thread 'main' panicked at 'not enough arguments',
618-
src/main.rs:26:13
619-
note: run with `RUST_BACKTRACE=1` environment variable to display
620-
a backtrace
617+
thread 'main' panicked at src/main.rs:26:13:
618+
not enough arguments
619+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
621620
```
622621

623622
This output is better: we now have a reasonable error message. However, we also
@@ -668,7 +667,7 @@ impl Config {
668667
Listing 12-9: Returning a <code>Result</code> from <code>Config::build</code>
669668

670669
Our `build` function returns a `Result` with a `Config` instance in the success
671-
case and an `&'static str` in the error case. Our error values will always be
670+
case and a string literal in the error case. Our error values will always be
672671
string literals that have the `'static` lifetime.
673672

674673
We’ve made two changes in the body of the function: instead of calling `panic!`
@@ -737,7 +736,7 @@ extra output. Let’s try it:
737736
```
738737
$ cargo run
739738
Compiling minigrep v0.1.0 (file:///projects/minigrep)
740-
Finished dev [unoptimized + debuginfo] target(s) in 0.48s
739+
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.48s
741740
Running `target/debug/minigrep`
742741
Problem parsing arguments: not enough arguments
743742
```
@@ -851,11 +850,16 @@ warning: unused `Result` that must be used
851850
--> src/main.rs:19:5
852851
|
853852
19 | run(config);
854-
| ^^^^^^^^^^^^
853+
| ^^^^^^^^^^^
855854
|
855+
= note: this `Result` may be an `Err` variant, which should be handled
856856
= note: `#[warn(unused_must_use)]` on by default
857-
= note: this `Result` may be an `Err` variant, which should be
858-
handled
857+
help: use `let _ = ...` to ignore the resulting value
858+
|
859+
19 | let _ = run(config);
860+
| +++++++
861+
862+
warning: `minigrep` (bin "minigrep") generated 1 warning
859863
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.71s
860864
Running `target/debug/minigrep the poem.txt`
861865
Searching for the
@@ -1129,7 +1133,7 @@ Now let’s run the test:
11291133
```
11301134
$ cargo test
11311135
Compiling minigrep v0.1.0 (file:///projects/minigrep)
1132-
Finished test [unoptimized + debuginfo] target(s) in 0.97s
1136+
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.97s
11331137
Running unittests src/lib.rs (target/debug/deps/minigrep-9cd200e5fac0fc94)
11341138
11351139
running 1 test
@@ -1138,9 +1142,10 @@ test tests::one_result ... FAILED
11381142
failures:
11391143
11401144
---- tests::one_result stdout ----
1141-
thread 'tests::one_result' panicked at 'assertion failed: `(left == right)`
1142-
left: `["safe, fast, productive."]`,
1143-
right: `[]`', src/lib.rs:47:9
1145+
thread 'tests::one_result' panicked at src/lib.rs:44:9:
1146+
assertion `left == right` failed
1147+
left: ["safe, fast, productive."]
1148+
right: []
11441149
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
11451150
11461151
@@ -1149,7 +1154,7 @@ failures:
11491154
11501155
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
11511156
1152-
error: test failed, to rerun pass '--lib'
1157+
error: test failed, to rerun pass `--lib`
11531158
```
11541159

11551160
Great, the test fails, exactly as we expected. Let’s get the test to pass!
@@ -1309,7 +1314,7 @@ should return exactly one line from the Emily Dickinson poem: *frog*.
13091314
```
13101315
$ cargo run -- frog poem.txt
13111316
Compiling minigrep v0.1.0 (file:///projects/minigrep)
1312-
Finished dev [unoptimized + debuginfo] target(s) in 0.38s
1317+
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.38s
13131318
Running `target/debug/minigrep frog poem.txt`
13141319
How public, like a frog
13151320
```
@@ -1318,7 +1323,8 @@ Cool! Now let’s try a word that will match multiple lines, like *body*:
13181323

13191324
```
13201325
$ cargo run -- body poem.txt
1321-
Finished dev [unoptimized + debuginfo] target(s) in 0.0s
1326+
Compiling minigrep v0.1.0 (file:///projects/minigrep)
1327+
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.0s
13221328
Running `target/debug/minigrep body poem.txt`
13231329
I'm nobody! Who are you?
13241330
Are you nobody, too?
@@ -1330,7 +1336,8 @@ word that isn’t anywhere in the poem, such as *monomorphization*:
13301336

13311337
```
13321338
$ cargo run -- monomorphization poem.txt
1333-
Finished dev [unoptimized + debuginfo] target(s) in 0.0s
1339+
Compiling minigrep v0.1.0 (file:///projects/minigrep)
1340+
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.0s
13341341
Running `target/debug/minigrep monomorphization poem.txt`
13351342
```
13361343

@@ -1600,14 +1607,14 @@ the word *to* in all lowercase:
16001607
```
16011608
$ cargo run -- to poem.txt
16021609
Compiling minigrep v0.1.0 (file:///projects/minigrep)
1603-
Finished dev [unoptimized + debuginfo] target(s) in 0.0s
1610+
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.0s
16041611
Running `target/debug/minigrep to poem.txt`
16051612
Are you nobody, too?
16061613
How dreary to be somebody!
16071614
```
16081615

16091616
Looks like that still works! Now let’s run the program with `IGNORE_CASE` set
1610-
to `1` but with the same query `to`:
1617+
to `1` but with the same query *to*:
16111618

16121619
```
16131620
$ IGNORE_CASE=1 cargo run -- to poem.txt

0 commit comments

Comments
 (0)