Skip to content

Commit 45c1a6d

Browse files
authored
Merge pull request #3959 from bzierk/ch12-ch14
Convert Chapters 12-14
2 parents 047226f + c7462f3 commit 45c1a6d

11 files changed

+108
-155
lines changed

src/ch12-01-accepting-command-line-arguments.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,13 @@ the iterator produces.
3939
The code in Listing 12-1 allows your `minigrep` program to read any command
4040
line arguments passed to it and then collect the values into a vector.
4141

42-
<span class="filename">Filename: src/main.rs</span>
42+
<Listing number="12-1" file-name="src/main.rs" caption="Collecting the command line arguments into a vector and printing them">
4343

4444
```rust
4545
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-01/src/main.rs}}
4646
```
4747

48-
<span class="caption">Listing 12-1: Collecting the command line arguments into
49-
a vector and printing them</span>
48+
</Listing>
5049

5150
First, we bring the `std::env` module into scope with a `use` statement so we
5251
can use its `args` function. Notice that the `std::env::args` function is
@@ -101,14 +100,13 @@ arguments. Now we need to save the values of the two arguments in variables so
101100
we can use the values throughout the rest of the program. We do that in Listing
102101
12-2.
103102

104-
<span class="filename">Filename: src/main.rs</span>
103+
<Listing number="12-2" file-name="src/main.rs" caption="Creating variables to hold the query argument and file path argument">
105104

106105
```rust,should_panic,noplayground
107106
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-02/src/main.rs}}
108107
```
109108

110-
<span class="caption">Listing 12-2: Creating variables to hold the query
111-
argument and file path argument</span>
109+
</Listing>
112110

113111
As we saw when we printed the vector, the program’s name takes up the first
114112
value in the vector at `args[0]`, so we’re starting arguments at index `1`. The

src/ch12-02-reading-a-file.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,24 @@ has an Emily Dickinson poem that will work well! Create a file called
77
*poem.txt* at the root level of your project, and enter the poem “I’m Nobody!
88
Who are you?”
99

10-
<span class="filename">Filename: poem.txt</span>
10+
<Listing number="12-3" file-name="poem.txt" caption="A poem by Emily Dickinson makes a good test case">
1111

1212
```text
1313
{{#include ../listings/ch12-an-io-project/listing-12-03/poem.txt}}
1414
```
1515

16-
<span class="caption">Listing 12-3: A poem by Emily Dickinson makes a good test
17-
case</span>
16+
</Listing>
1817

1918
With the text in place, edit *src/main.rs* and add code to read the file, as
2019
shown in Listing 12-4.
2120

22-
<span class="filename">Filename: src/main.rs</span>
21+
<Listing number="12-4" file-name="src/main.rs" caption="Reading the contents of the file specified by the second argument">
2322

2423
```rust,should_panic,noplayground
2524
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-04/src/main.rs:here}}
2625
```
2726

28-
<span class="caption">Listing 12-4: Reading the contents of the file specified
29-
by the second argument</span>
27+
</Listing>
3028

3129
First, we bring in a relevant part of the standard library with a `use`
3230
statement: we need `std::fs` to handle files.

src/ch12-03-improving-error-handling-and-modularity.md

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,13 @@ We’ll extract the functionality for parsing arguments into a function that
7070
*src/lib.rs*. Listing 12-5 shows the new start of `main` that calls a new
7171
function `parse_config`, which we’ll define in *src/main.rs* for the moment.
7272

73-
<span class="filename">Filename: src/main.rs</span>
73+
<Listing number="12-5" file-name="src/main.rs" caption="Extracting a `parse_config` function from `main`">
7474

7575
```rust,ignore
7676
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-05/src/main.rs:here}}
7777
```
7878

79-
<span class="caption">Listing 12-5: Extracting a `parse_config` function from
80-
`main`</span>
79+
</Listing>
8180

8281
We’re still collecting the command line arguments into a vector, but instead of
8382
assigning the argument value at index 1 to the variable `query` and the
@@ -112,14 +111,13 @@ other and what their purpose is.
112111

113112
Listing 12-6 shows the improvements to the `parse_config` function.
114113

115-
<span class="filename">Filename: src/main.rs</span>
114+
<Listing number="12-6" file-name="src/main.rs" caption="Refactoring `parse_config` to return an instance of a `Config` struct">
116115

117116
```rust,should_panic,noplayground
118117
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-06/src/main.rs:here}}
119118
```
120119

121-
<span class="caption">Listing 12-6: Refactoring `parse_config` to return an
122-
instance of a `Config` struct</span>
120+
</Listing>
123121

124122
We’ve added a struct named `Config` defined to have fields named `query` and
125123
`file_path`. The signature of `parse_config` now indicates that it returns a
@@ -179,14 +177,13 @@ changing `parse_config` into a `new` function associated with `Config`, we’ll
179177
be able to create instances of `Config` by calling `Config::new`. Listing 12-7
180178
shows the changes we need to make.
181179

182-
<span class="filename">Filename: src/main.rs</span>
180+
<Listing number="12-7" file-name="src/main.rs" caption="Changing `parse_config` into `Config::new`">
183181

184182
```rust,should_panic,noplayground
185183
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-07/src/main.rs:here}}
186184
```
187185

188-
<span class="caption">Listing 12-7: Changing `parse_config` into
189-
`Config::new`</span>
186+
</Listing>
190187

191188
We’ve updated `main` where we were calling `parse_config` to instead call
192189
`Config::new`. We’ve changed the name of `parse_config` to `new` and moved it
@@ -214,14 +211,13 @@ In Listing 12-8, we add a check in the `new` function that will verify that the
214211
slice is long enough before accessing index 1 and 2. If the slice isn’t long
215212
enough, the program panics and displays a better error message.
216213

217-
<span class="filename">Filename: src/main.rs</span>
214+
<Listing number="12-8" file-name="src/main.rs" caption="Adding a check for the number of arguments">
218215

219216
```rust,ignore
220217
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-08/src/main.rs:here}}
221218
```
222219

223-
<span class="caption">Listing 12-8: Adding a check for the number of
224-
arguments</span>
220+
</Listing>
225221

226222
This code is similar to [the `Guess::new` function we wrote in Listing
227223
9-13][ch9-custom-types]<!-- ignore -->, where we called `panic!` when the
@@ -265,14 +261,13 @@ function we’re now calling `Config::build` and the body of the function needed
265261
to return a `Result`. Note that this won’t compile until we update `main` as
266262
well, which we’ll do in the next listing.
267263

268-
<span class="filename">Filename: src/main.rs</span>
264+
<Listing number="12-9" file-name="src/main.rs" caption="Returning a `Result` from `Config::build`">
269265

270266
```rust,ignore,does_not_compile
271267
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-09/src/main.rs:here}}
272268
```
273269

274-
<span class="caption">Listing 12-9: Returning a `Result` from
275-
`Config::build`</span>
270+
</Listing>
276271

277272
Our `build` function returns a `Result` with a `Config` instance in the success
278273
case and a `&'static str` in the error case. Our error values will always be
@@ -299,14 +294,13 @@ tool with a nonzero error code away from `panic!` and instead implement it by
299294
hand. A nonzero exit status is a convention to signal to the process that
300295
called our program that the program exited with an error state.
301296

302-
<span class="filename">Filename: src/main.rs</span>
297+
<Listing number="12-10" file-name="src/main.rs" caption="Exiting with an error code if building a `Config` fails">
303298

304299
```rust,ignore
305300
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-10/src/main.rs:here}}
306301
```
307302

308-
<span class="caption">Listing 12-10: Exiting with an error code if building a
309-
`Config` fails</span>
303+
</Listing>
310304

311305
In this listing, we’ve used a method we haven’t covered in detail yet:
312306
`unwrap_or_else`, which is defined on `Result<T, E>` by the standard library.
@@ -350,14 +344,13 @@ Listing 12-11 shows the extracted `run` function. For now, we’re just making
350344
the small, incremental improvement of extracting the function. We’re still
351345
defining the function in *src/main.rs*.
352346

353-
<span class="filename">Filename: src/main.rs</span>
347+
<Listing number="12-11" file-name="src/main.rs" caption="Extracting a `run` function containing the rest of the program logic">
354348

355349
```rust,ignore
356350
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-11/src/main.rs:here}}
357351
```
358352

359-
<span class="caption">Listing 12-11: Extracting a `run` function containing the
360-
rest of the program logic</span>
353+
</Listing>
361354

362355
The `run` function now contains all the remaining logic from `main`, starting
363356
from reading the file. The `run` function takes the `Config` instance as an
@@ -373,14 +366,13 @@ us further consolidate the logic around handling errors into `main` in a
373366
user-friendly way. Listing 12-12 shows the changes we need to make to the
374367
signature and body of `run`.
375368

376-
<span class="filename">Filename: src/main.rs</span>
369+
<Listing number="12-12" file-name="src/main.rs" caption="Changing the `run` function to return `Result`">
377370

378371
```rust,ignore
379372
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-12/src/main.rs:here}}
380373
```
381374

382-
<span class="caption">Listing 12-12: Changing the `run` function to return
383-
`Result`</span>
375+
</Listing>
384376

385377
We’ve made three significant changes here. First, we changed the return type of
386378
the `run` function to `Result<(), Box<dyn Error>>`. This function previously
@@ -458,14 +450,13 @@ The contents of *src/lib.rs* should have the signatures shown in Listing 12-13
458450
(we’ve omitted the bodies of the functions for brevity). Note that this won’t
459451
compile until we modify *src/main.rs* in Listing 12-14.
460452

461-
<span class="filename">Filename: src/lib.rs</span>
453+
<Listing number="12-13" file-name="src/lib.rs" caption="Moving `Config` and `run` into *src/lib.rs*">
462454

463455
```rust,ignore,does_not_compile
464456
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-13/src/lib.rs:here}}
465457
```
466458

467-
<span class="caption">Listing 12-13: Moving `Config` and `run` into
468-
*src/lib.rs*</span>
459+
</Listing>
469460

470461
We’ve made liberal use of the `pub` keyword: on `Config`, on its fields and its
471462
`build` method, and on the `run` function. We now have a library crate that has
@@ -474,14 +465,13 @@ a public API we can test!
474465
Now we need to bring the code we moved to *src/lib.rs* into the scope of the
475466
binary crate in *src/main.rs*, as shown in Listing 12-14.
476467

477-
<span class="filename">Filename: src/main.rs</span>
468+
<Listing number="12-14" file-name="src/main.rs" caption="Using the `minigrep` library crate in *src/main.rs*">
478469

479470
```rust,ignore
480471
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-14/src/main.rs:here}}
481472
```
482473

483-
<span class="caption">Listing 12-14: Using the `minigrep` library crate in
484-
*src/main.rs*</span>
474+
</Listing>
485475

486476
We add a `use minigrep::Config` line to bring the `Config` type from the
487477
library crate into the binary crate’s scope, and we prefix the `run` function

src/ch12-04-testing-the-librarys-functionality.md

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,13 @@ behavior we want the `search` function to have: it will take a query and the
3535
text to search, and it will return only the lines from the text that contain
3636
the query. Listing 12-15 shows this test, which won’t compile yet.
3737

38-
<span class="filename">Filename: src/lib.rs</span>
38+
<Listing number="12-15" file-name="src/lib.rs" caption="Creating a failing test for the `search` function we wish we had">
3939

4040
```rust,ignore,does_not_compile
4141
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-15/src/lib.rs:here}}
4242
```
4343

44-
<span class="caption">Listing 12-15: Creating a failing test for the `search`
45-
function we wish we had</span>
44+
</Listing>
4645

4746
This test searches for the string `"duct"`. The text we’re searching is three
4847
lines, only one of which contains `"duct"` (Note that the backslash after the
@@ -58,14 +57,13 @@ vector, as shown in Listing 12-16. Then the test should compile and fail
5857
because an empty vector doesn’t match a vector containing the line `"safe,
5958
fast, productive."`
6059

61-
<span class="filename">Filename: src/lib.rs</span>
60+
<Listing number="12-16" file-name="src/lib.rs" caption="Defining just enough of the `search` function so our test will compile">
6261

6362
```rust,noplayground
6463
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-16/src/lib.rs:here}}
6564
```
6665

67-
<span class="caption">Listing 12-16: Defining just enough of the `search`
68-
function so our test will compile</span>
66+
</Listing>
6967

7068
Notice that we need to define an explicit lifetime `'a` in the signature of
7169
`search` and use that lifetime with the `contents` argument and the return
@@ -128,14 +126,13 @@ Rust has a helpful method to handle line-by-line iteration of strings,
128126
conveniently named `lines`, that works as shown in Listing 12-17. Note this
129127
won’t compile yet.
130128

131-
<span class="filename">Filename: src/lib.rs</span>
129+
<Listing number="12-17" file-name="src/lib.rs" caption="Iterating through each line in `contents`">
132130

133131
```rust,ignore,does_not_compile
134132
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-17/src/lib.rs:here}}
135133
```
136134

137-
<span class="caption">Listing 12-17: Iterating through each line in `contents`
138-
</span>
135+
</Listing>
139136

140137
The `lines` method returns an iterator. We’ll talk about iterators in depth in
141138
[Chapter 13][ch13-iterators]<!-- ignore -->, but recall that you saw this way
@@ -149,14 +146,13 @@ Fortunately, strings have a helpful method named `contains` that does this for
149146
us! Add a call to the `contains` method in the `search` function, as shown in
150147
Listing 12-18. Note this still won’t compile yet.
151148

152-
<span class="filename">Filename: src/lib.rs</span>
149+
<Listing number="12-18" file-name="src/lib.rs" caption="Adding functionality to see whether the line contains the string in `query`">
153150

154151
```rust,ignore,does_not_compile
155152
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-18/src/lib.rs:here}}
156153
```
157154

158-
<span class="caption">Listing 12-18: Adding functionality to see whether the
159-
line contains the string in `query`</span>
155+
</Listing>
160156

161157
At the moment, we’re building up functionality. To get it to compile, we need
162158
to return a value from the body as we indicated we would in the function
@@ -169,14 +165,13 @@ to return. For that, we can make a mutable vector before the `for` loop and
169165
call the `push` method to store a `line` in the vector. After the `for` loop,
170166
we return the vector, as shown in Listing 12-19.
171167

172-
<span class="filename">Filename: src/lib.rs</span>
168+
<Listing number="12-19" file-name="src/lib.rs" caption="Storing the lines that match so we can return them">
173169

174170
```rust,ignore
175171
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-19/src/lib.rs:here}}
176172
```
177173

178-
<span class="caption">Listing 12-19: Storing the lines that match so we can
179-
return them</span>
174+
</Listing>
180175

181176
Now the `search` function should return only the lines that contain `query`,
182177
and our test should pass. Let’s run the test:

src/ch12-05-working-with-environment-variables.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@ the new `search_case_insensitive` function and rename our old test from
1616
`one_result` to `case_sensitive` to clarify the differences between the two
1717
tests, as shown in Listing 12-20.
1818

19-
<span class="filename">Filename: src/lib.rs</span>
19+
<Listing number="12-20" file-name="src/lib.rs" caption="Adding a new failing test for the case-insensitive function we’re about to add">
2020

2121
```rust,ignore,does_not_compile
2222
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-20/src/lib.rs:here}}
2323
```
2424

25-
<span class="caption">Listing 12-20: Adding a new failing test for the
26-
case-insensitive function we’re about to add</span>
25+
</Listing>
2726

2827
Note that we’ve edited the old test’s `contents` too. We’ve added a new line
2928
with the text `"Duct tape."` using a capital D that shouldn’t match the query
@@ -48,14 +47,13 @@ the same as the `search` function. The only difference is that we’ll lowercase
4847
the `query` and each `line` so whatever the case of the input arguments,
4948
they’ll be the same case when we check whether the line contains the query.
5049

51-
<span class="filename">Filename: src/lib.rs</span>
50+
<Listing number="12-21" file-name="src/lib.rs" caption="Defining the `search_case_insensitive` function to lowercase the query and the line before comparing them">
5251

5352
```rust,noplayground
5453
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-21/src/lib.rs:here}}
5554
```
5655

57-
<span class="caption">Listing 12-21: Defining the `search_case_insensitive`
58-
function to lowercase the query and the line before comparing them</span>
56+
</Listing>
5957

6058
First, we lowercase the `query` string and store it in a shadowed variable with
6159
the same name. Calling `to_lowercase` on the query is necessary so no
@@ -101,14 +99,13 @@ function to check the `ignore_case` field’s value and use that to decide
10199
whether to call the `search` function or the `search_case_insensitive`
102100
function, as shown in Listing 12-22. This still won’t compile yet.
103101

104-
<span class="filename">Filename: src/lib.rs</span>
102+
<Listing number="12-22" file-name="src/lib.rs" caption="Calling either `search` or `search_case_insensitive` based on the value in `config.ignore_case`">
105103

106104
```rust,ignore,does_not_compile
107105
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-22/src/lib.rs:there}}
108106
```
109107

110-
<span class="caption">Listing 12-22: Calling either `search` or
111-
`search_case_insensitive` based on the value in `config.ignore_case`</span>
108+
</Listing>
112109

113110
Finally, we need to check for the environment variable. The functions for
114111
working with environment variables are in the `env` module in the standard
@@ -117,14 +114,13 @@ we’ll use the `var` function from the `env` module to check if any value
117114
has been set for an environment variable named `IGNORE_CASE`, as shown in
118115
Listing 12-23.
119116

120-
<span class="filename">Filename: src/lib.rs</span>
117+
<Listing number="12-23" file-name="src/lib.rs" caption="Checking for any value in an environment variable named `IGNORE_CASE`">
121118

122119
```rust,noplayground
123120
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-23/src/lib.rs:here}}
124121
```
125122

126-
<span class="caption">Listing 12-23: Checking for any value in an environment
127-
variable named `IGNORE_CASE`</span>
123+
</Listing>
128124

129125
Here, we create a new variable `ignore_case`. To set its value, we call the
130126
`env::var` function and pass it the name of the `IGNORE_CASE` environment

0 commit comments

Comments
 (0)