@@ -70,14 +70,13 @@ We’ll extract the functionality for parsing arguments into a function that
70
70
* src/lib.rs* . Listing 12-5 shows the new start of ` main ` that calls a new
71
71
function ` parse_config ` , which we’ll define in * src/main.rs* for the moment.
72
72
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` " >
74
74
75
75
``` rust,ignore
76
76
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-05/src/main.rs:here}}
77
77
```
78
78
79
- <span class =" caption " >Listing 12-5: Extracting a ` parse_config ` function from
80
- ` main ` </span >
79
+ </Listing >
81
80
82
81
We’re still collecting the command line arguments into a vector, but instead of
83
82
assigning the argument value at index 1 to the variable ` query ` and the
@@ -112,14 +111,13 @@ other and what their purpose is.
112
111
113
112
Listing 12-6 shows the improvements to the ` parse_config ` function.
114
113
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 " >
116
115
117
116
``` rust,should_panic,noplayground
118
117
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-06/src/main.rs:here}}
119
118
```
120
119
121
- <span class =" caption " >Listing 12-6: Refactoring ` parse_config ` to return an
122
- instance of a ` Config ` struct</span >
120
+ </Listing >
123
121
124
122
We’ve added a struct named ` Config ` defined to have fields named ` query ` and
125
123
` 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
179
177
be able to create instances of ` Config ` by calling ` Config::new ` . Listing 12-7
180
178
shows the changes we need to make.
181
179
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` " >
183
181
184
182
``` rust,should_panic,noplayground
185
183
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-07/src/main.rs:here}}
186
184
```
187
185
188
- <span class =" caption " >Listing 12-7: Changing ` parse_config ` into
189
- ` Config::new ` </span >
186
+ </Listing >
190
187
191
188
We’ve updated ` main ` where we were calling ` parse_config ` to instead call
192
189
` 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
214
211
slice is long enough before accessing index 1 and 2. If the slice isn’t long
215
212
enough, the program panics and displays a better error message.
216
213
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 " >
218
215
219
216
``` rust,ignore
220
217
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-08/src/main.rs:here}}
221
218
```
222
219
223
- <span class =" caption " >Listing 12-8: Adding a check for the number of
224
- arguments</span >
220
+ </Listing >
225
221
226
222
This code is similar to [ the ` Guess::new ` function we wrote in Listing
227
223
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
265
261
to return a ` Result ` . Note that this won’t compile until we update ` main ` as
266
262
well, which we’ll do in the next listing.
267
263
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` " >
269
265
270
266
``` rust,ignore,does_not_compile
271
267
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-09/src/main.rs:here}}
272
268
```
273
269
274
- <span class =" caption " >Listing 12-9: Returning a ` Result ` from
275
- ` Config::build ` </span >
270
+ </Listing >
276
271
277
272
Our ` build ` function returns a ` Result ` with a ` Config ` instance in the success
278
273
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
299
294
hand. A nonzero exit status is a convention to signal to the process that
300
295
called our program that the program exited with an error state.
301
296
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 " >
303
298
304
299
``` rust,ignore
305
300
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-10/src/main.rs:here}}
306
301
```
307
302
308
- <span class =" caption " >Listing 12-10: Exiting with an error code if building a
309
- ` Config ` fails</span >
303
+ </Listing >
310
304
311
305
In this listing, we’ve used a method we haven’t covered in detail yet:
312
306
` 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
350
344
the small, incremental improvement of extracting the function. We’re still
351
345
defining the function in * src/main.rs* .
352
346
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 " >
354
348
355
349
``` rust,ignore
356
350
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-11/src/main.rs:here}}
357
351
```
358
352
359
- <span class =" caption " >Listing 12-11: Extracting a ` run ` function containing the
360
- rest of the program logic</span >
353
+ </Listing >
361
354
362
355
The ` run ` function now contains all the remaining logic from ` main ` , starting
363
356
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
373
366
user-friendly way. Listing 12-12 shows the changes we need to make to the
374
367
signature and body of ` run ` .
375
368
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` " >
377
370
378
371
``` rust,ignore
379
372
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-12/src/main.rs:here}}
380
373
```
381
374
382
- <span class =" caption " >Listing 12-12: Changing the ` run ` function to return
383
- ` Result ` </span >
375
+ </Listing >
384
376
385
377
We’ve made three significant changes here. First, we changed the return type of
386
378
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
458
450
(we’ve omitted the bodies of the functions for brevity). Note that this won’t
459
451
compile until we modify * src/main.rs* in Listing 12-14.
460
452
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* " >
462
454
463
455
``` rust,ignore,does_not_compile
464
456
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-13/src/lib.rs:here}}
465
457
```
466
458
467
- <span class =" caption " >Listing 12-13: Moving ` Config ` and ` run ` into
468
- * src/lib.rs* </span >
459
+ </Listing >
469
460
470
461
We’ve made liberal use of the ` pub ` keyword: on ` Config ` , on its fields and its
471
462
` 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!
474
465
Now we need to bring the code we moved to * src/lib.rs* into the scope of the
475
466
binary crate in * src/main.rs* , as shown in Listing 12-14.
476
467
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* " >
478
469
479
470
``` rust,ignore
480
471
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-14/src/main.rs:here}}
481
472
```
482
473
483
- <span class =" caption " >Listing 12-14: Using the ` minigrep ` library crate in
484
- * src/main.rs* </span >
474
+ </Listing >
485
475
486
476
We add a ` use minigrep::Config ` line to bring the ` Config ` type from the
487
477
library crate into the binary crate’s scope, and we prefix the ` run ` function
0 commit comments