@@ -29,13 +29,13 @@ return may differ.
29
29
Let’s call a function that returns a ` Result ` value because the function could
30
30
fail. In Listing 9-3 we try to open a file.
31
31
32
- <span class = " filename " >Filename: src/main.rs</ span >
32
+ <Listing number = " 9-3 " file-name = " src/main.rs " caption = " Opening a file " >
33
33
34
34
``` rust
35
35
{{#rustdoc_include .. / listings / ch09 - error - handling / listing - 09 - 03 / src / main . rs}}
36
36
```
37
37
38
- <span class = " caption " >Listing 9-3: Opening a file</ span >
38
+ </ Listing >
39
39
40
40
The return type of ` File::open ` is a ` Result<T, E> ` . The generic parameter ` T `
41
41
has been filled in by the implementation of ` File::open ` with the type of the
@@ -59,14 +59,13 @@ on the value `File::open` returns. Listing 9-4 shows one way to handle the
59
59
` Result ` using a basic tool, the ` match ` expression that we discussed in
60
60
Chapter 6.
61
61
62
- <span class = " filename " >Filename: src/main.rs</ span >
62
+ <Listing number = " 9-4 " file-name = " src/main.rs " caption = " Using a `match` expression to handle the `Result` variants that might be returned " >
63
63
64
64
``` rust,should_panic
65
65
{{#rustdoc_include ../listings/ch09-error-handling/listing-09-04/src/main.rs}}
66
66
```
67
67
68
- <span class =" caption " >Listing 9-4: Using a ` match ` expression to handle the
69
- ` Result ` variants that might be returned</span >
68
+ </Listing >
70
69
71
70
Note that, like the ` Option ` enum, the ` Result ` enum and its variants have been
72
71
brought into scope by the prelude, so we don’t need to specify ` Result:: `
@@ -98,7 +97,7 @@ reason—for example, because we didn’t have permission to open the file—we
98
97
want the code to ` panic! ` in the same way it did in Listing 9-4. For this, we
99
98
add an inner ` match ` expression, shown in Listing 9-5.
100
99
101
- <span class = " filename " >Filename: src/main.rs</ span >
100
+ <Listing number = " 9-5 " file-name = " src/main.rs " caption = " Handling different kinds of errors in different ways " >
102
101
103
102
<!-- ignore this test because otherwise it creates hello.txt which causes other
104
103
tests to fail lol -->
@@ -107,8 +106,7 @@ tests to fail lol -->
107
106
{{#rustdoc_include ../listings/ch09-error-handling/listing-09-05/src/main.rs}}
108
107
```
109
108
110
- <span class =" caption " >Listing 9-5: Handling different kinds of errors in
111
- different ways</span >
109
+ </Listing >
112
110
113
111
The type of the value that ` File::open ` returns inside the ` Err ` variant is
114
112
` io::Error ` , which is a struct provided by the standard library. This struct
@@ -172,12 +170,14 @@ Listing 9-4. If the `Result` value is the `Ok` variant, `unwrap` will return
172
170
the value inside the `Ok`. If the `Result` is the `Err` variant, `unwrap` will
173
171
call the `panic!` macro for us. Here is an example of `unwrap` in action:
174
172
175
- <span class="filename">Filename: src/main.rs</span >
173
+ <Listing file-name=" src/main.rs" >
176
174
177
175
```rust,should_panic
178
176
{{#rustdoc_include ../listings/ch09-error-handling/no-listing-04-unwrap/src/main.rs}}
179
177
```
180
178
179
+ </Listing >
180
+
181
181
If we run this code without a * hello.txt* file, we’ll see an error message from
182
182
the ` panic! ` call that the ` unwrap ` method makes:
183
183
@@ -197,12 +197,14 @@ Using `expect` instead of `unwrap` and providing good error messages can convey
197
197
your intent and make tracking down the source of a panic easier. The syntax of
198
198
` expect ` looks like this:
199
199
200
- <span class = " filename " >Filename: src/main.rs</ span >
200
+ <Listing file-name = " src/main.rs " >
201
201
202
202
``` rust,should_panic
203
203
{{#rustdoc_include ../listings/ch09-error-handling/no-listing-05-expect/src/main.rs}}
204
204
```
205
205
206
+ </Listing >
207
+
206
208
We use ` expect ` in the same way as ` unwrap ` : to return the file handle or call
207
209
the ` panic! ` macro. The error message used by ` expect ` in its call to ` panic! `
208
210
will be the parameter that we pass to ` expect ` , rather than the default
@@ -237,7 +239,7 @@ For example, Listing 9-6 shows a function that reads a username from a file. If
237
239
the file doesn’t exist or can’t be read, this function will return those errors
238
240
to the code that called the function.
239
241
240
- <span class = " filename " >Filename: src/main.rs</ span >
242
+ <Listing number = " 9-6 " file-name = " src/main.rs " caption = " A function that returns errors to the calling code using `match` " >
241
243
242
244
<!-- Deliberately not using rustdoc_include here; the `main` function in the
243
245
file panics. We do want to include it for reader experimentation purposes, but
@@ -247,8 +249,7 @@ don't want to include it for rustdoc testing purposes. -->
247
249
{{#include .. / listings / ch09 - error - handling / listing - 09 - 06 / src / main . rs: here }}
248
250
```
249
251
250
- <span class =" caption " >Listing 9-6: A function that returns errors to the
251
- calling code using ` match ` </span >
252
+ </Listing >
252
253
253
254
This function can be written in a much shorter way, but we’re going to start by
254
255
doing a lot of it manually in order to explore error handling; at the end,
@@ -307,7 +308,7 @@ Listing 9-7 shows an implementation of `read_username_from_file` that has the
307
308
same functionality as in Listing 9-6, but this implementation uses the ` ? `
308
309
operator.
309
310
310
- <span class = " filename " >Filename: src/main.rs</ span >
311
+ <Listing number = " 9-7 " file-name = " src/main.rs " caption = " A function that returns errors to the calling code using the `?` operator " >
311
312
312
313
<!-- Deliberately not using rustdoc_include here; the `main` function in the
313
314
file panics. We do want to include it for reader experimentation purposes, but
@@ -317,8 +318,7 @@ don't want to include it for rustdoc testing purposes. -->
317
318
{{#include .. / listings / ch09 - error - handling / listing - 09 - 07 / src / main . rs: here }}
318
319
```
319
320
320
- <span class =" caption " >Listing 9-7: A function that returns errors to the
321
- calling code using the ` ? ` operator</span >
321
+ </Listing >
322
322
323
323
The ` ? ` placed after a ` Result ` value is defined to work in almost the same way
324
324
as the ` match ` expressions we defined to handle the ` Result ` values in Listing
@@ -355,7 +355,7 @@ The `?` operator eliminates a lot of boilerplate and makes this function’s
355
355
implementation simpler. We could even shorten this code further by chaining
356
356
method calls immediately after the ` ? ` , as shown in Listing 9-8.
357
357
358
- <span class = " filename " >Filename: src/main.rs</ span >
358
+ <Listing number = " 9-8 " file-name = " src/main.rs " caption = " Chaining method calls after the `?` operator " >
359
359
360
360
<!-- Deliberately not using rustdoc_include here; the `main` function in the
361
361
file panics. We do want to include it for reader experimentation purposes, but
@@ -365,8 +365,7 @@ don't want to include it for rustdoc testing purposes. -->
365
365
{{#include .. / listings / ch09 - error - handling / listing - 09 - 08 / src / main . rs: here }}
366
366
```
367
367
368
- <span class =" caption " >Listing 9-8: Chaining method calls after the ` ? `
369
- operator</span >
368
+ </Listing >
370
369
371
370
We’ve moved the creation of the new ` String ` in ` username ` to the beginning of
372
371
the function; that part hasn’t changed. Instead of creating a variable
@@ -379,7 +378,7 @@ this is just a different, more ergonomic way to write it.
379
378
380
379
Listing 9-9 shows a way to make this even shorter using ` fs::read_to_string ` .
381
380
382
- <span class = " filename " >Filename: src/main.rs</ span >
381
+ <Listing number = " 9-9 " file-name = " src/main.rs " caption = " Using `fs::read_to_string` instead of opening and then reading the file " >
383
382
384
383
<!-- Deliberately not using rustdoc_include here; the `main` function in the
385
384
file panics. We do want to include it for reader experimentation purposes, but
@@ -389,8 +388,7 @@ don't want to include it for rustdoc testing purposes. -->
389
388
{{#include .. / listings / ch09 - error - handling / listing - 09 - 09 / src / main . rs: here }}
390
389
```
391
390
392
- <span class =" caption " >Listing 9-9: Using ` fs::read_to_string ` instead of
393
- opening and then reading the file</span >
391
+ </Listing >
394
392
395
393
Reading a file into a string is a fairly common operation, so the standard
396
394
library provides the convenient ` fs::read_to_string ` function that opens the
@@ -413,14 +411,13 @@ In Listing 9-10, let’s look at the error we’ll get if we use the `?` operato
413
411
in a ` main ` function with a return type that is incompatible with the type of
414
412
the value we use ` ? ` on.
415
413
416
- <span class = " filename " >Filename: src/main.rs</ span >
414
+ <Listing number = " 9-10 " file-name = " src/main.rs " caption = " Attempting to use the `?` in the `main` function that returns `()` won’t compile. " >
417
415
418
416
``` rust,ignore,does_not_compile
419
417
{{#rustdoc_include ../listings/ch09-error-handling/listing-09-10/src/main.rs}}
420
418
```
421
419
422
- <span class =" caption " >Listing 9-10: Attempting to use the ` ? ` in the ` main `
423
- function that returns ` () ` won’t compile.</span >
420
+ </Listing >
424
421
425
422
This code opens a file, which might fail. The ` ? ` operator follows the ` Result `
426
423
value returned by ` File::open ` , but this ` main ` function has the return type of
@@ -451,12 +448,13 @@ resultant value of the expression, and the function continues. Listing 9-11 has
451
448
an example of a function that finds the last character of the first line in the
452
449
given text.
453
450
451
+ <Listing number =" 9-11 " caption =" Using the `?` operator on an `Option<T>` value " >
452
+
454
453
``` rust
455
454
{{#rustdoc_include .. / listings / ch09 - error - handling / listing - 09 - 11 / src / main . rs: here }}
456
455
```
457
456
458
- <span class =" caption " >Listing 9-11: Using the ` ? ` operator on an ` Option<T> `
459
- value</span >
457
+ </Listing >
460
458
461
459
This function returns ` Option<char> ` because it’s possible that there is a
462
460
character there, but it’s also possible that there isn’t. This code takes the
@@ -496,14 +494,13 @@ from Listing 9-10, but we’ve changed the return type of `main` to be
496
494
` Result<(), Box<dyn Error>> ` and added a return value ` Ok(()) ` to the end. This
497
495
code will now compile.
498
496
499
- <span class = " filename " >Filename: src/main.rs</ span >
497
+ <Listing number = " 9-12 " file-name = " src/main.rs " caption = " Changing `main` to return `Result<(), E>` allows the use of the `?` operator on `Result` values. " >
500
498
501
499
``` rust,ignore
502
500
{{#rustdoc_include ../listings/ch09-error-handling/listing-09-12/src/main.rs}}
503
501
```
504
502
505
- <span class =" caption " >Listing 9-12: Changing ` main ` to return ` Result<(), E> `
506
- allows the use of the ` ? ` operator on ` Result ` values.</span >
503
+ </Listing >
507
504
508
505
The ` Box<dyn Error> ` type is a * trait object* , which we’ll talk about in the
509
506
[ “Using Trait Objects that Allow for Values of Different
0 commit comments