]> git.proxmox.com Git - rustc.git/blob - src/doc/book/second-edition/nostarch/chapter09.md
New upstream version 1.23.0+dfsg1
[rustc.git] / src / doc / book / second-edition / nostarch / chapter09.md
1
2 [TOC]
3
4 # Error Handling
5
6 Rust’s commitment to reliability extends to error handling. Errors are a fact
7 of life in software, so Rust has a number of features for handling situations
8 in which something goes wrong. In many cases, Rust requires you to acknowledge
9 the possibility of an error occurring and take some action before your code
10 will compile. This requirement makes your program more robust by ensuring that
11 you’ll discover errors and handle them appropriately before you’ve deployed
12 your code to production!
13
14 Rust groups errors into two major categories: *recoverable* and *unrecoverable*
15 errors. Recoverable errors are situations in which it’s reasonable to report
16 the problem to the user and retry the operation, like a file not found error.
17 Unrecoverable errors are always symptoms of bugs, like trying to access a
18 location beyond the end of an array.
19
20 Most languages don’t distinguish between these two kinds of errors and handle
21 both in the same way using mechanisms like exceptions. Rust doesn’t have
22 exceptions. Instead, it has the value `Result<T, E>` for recoverable errors and
23 the `panic!` macro that stops execution when it encounters unrecoverable
24 errors. This chapter covers calling `panic!` first and then talks about
25 returning `Result<T, E>` values. Additionally, we’ll explore considerations to
26 take into account when deciding whether to try to recover from an error or to
27 stop execution.
28
29 ## Unrecoverable Errors with `panic!`
30
31 Sometimes, bad things happen in your code, and there’s nothing you can do about
32 it. In these cases, Rust has the `panic!` macro. When the `panic!` macro
33 executes, your program will print a failure message, unwind and clean up the
34 stack, and then quit. The most common situation this occurs in is when a bug of
35 some kind has been detected, and it’s not clear to the programmer how to handle
36 the error.
37
38 > ### Unwinding the Stack or Aborting in Response to a `panic!`
39 >
40 > By default, when a `panic!` occurs, the program starts *unwinding*, which
41 > means Rust walks back up the stack and cleans up the data from each function
42 > it encounters. But this walking back and cleanup is a lot of work. The
43 > alternative is to immediately *abort*, which ends the program without
44 > cleaning up. Memory that the program was using will then need to be cleaned
45 > up by the operating system. If in your project you need to make the resulting
46 > binary as small as possible, you can switch from unwinding to aborting on
47 > panic by adding `panic = 'abort'` to the appropriate `[profile]` sections in
48 > your *Cargo.toml* file. For example, if you want to abort on panic in release
49 > mode, add this:
50 >
51 > ```
52 > [profile.release]
53 > panic = 'abort'
54 > ```
55
56 Let’s try calling `panic!` in a simple program:
57
58 Filename: src/main.rs
59
60 ```
61 fn main() {
62 panic!("crash and burn");
63 }
64 ```
65
66 When you run the program, you’ll see something like this:
67
68 ```
69 $ cargo run
70 Compiling panic v0.1.0 (file:///projects/panic)
71 Finished dev [unoptimized + debuginfo] target(s) in 0.25 secs
72 Running `target/debug/panic`
73 thread 'main' panicked at 'crash and burn', src/main.rs:2
74 note: Run with `RUST_BACKTRACE=1` for a backtrace.
75 error: Process didn't exit successfully: `target/debug/panic` (exit code: 101)
76 ```
77
78 The call to `panic!` causes the error message contained in the last three
79 lines. The first line shows our panic message and the place in our source code
80 where the panic occurred: *src/main.rs:2* indicates that it’s the second line
81 of our *src/main.rs* file.
82
83 In this case, the line indicated is part of our code, and if we go to that
84 line, we see the `panic!` macro call. In other cases, the `panic!` call might
85 be in code that our code calls. The filename and line number reported by the
86 error message will be someone else’s code where the `panic!` macro is called,
87 not the line of our code that eventually led to the `panic!` call. We can use
88 the backtrace of the functions the `panic!` call came from to figure out the
89 part of our code that is causing the problem. We’ll discuss what a backtrace is
90 in more detail next.
91
92 ### Using a `panic!` Backtrace
93
94 Let’s look at another example to see what it’s like when a `panic!` call comes
95 from a library because of a bug in our code instead of from our code calling
96 the macro directly. Listing 9-1 has some code that attempts to access an
97 element by index in a vector:
98
99 Filename: src/main.rs
100
101 ```
102 fn main() {
103 let v = vec![1, 2, 3];
104
105 v[100];
106 }
107 ```
108
109 Listing 9-1: Attempting to access an element beyond the end of a vector, which
110 will cause a `panic!`
111
112 Here, we’re attempting to access the hundredth element of our vector, but it
113 has only three elements. In this situation, Rust will panic. Using `[]` is
114 supposed to return an element, but if you pass an invalid index, there’s no
115 element that Rust could return here that would be correct.
116
117 Other languages, like C, will attempt to give you exactly what you asked for in
118 this situation, even though it isn’t what you want: you’ll get whatever is at
119 the location in memory that would correspond to that element in the vector,
120 even though the memory doesn’t belong to the vector. This is called a *buffer
121 overread* and can lead to security vulnerabilities if an attacker is able to
122 manipulate the index in such a way as to read data they shouldn’t be allowed to
123 that is stored after the array.
124
125 To protect your program from this sort of vulnerability, if you try to read an
126 element at an index that doesn’t exist, Rust will stop execution and refuse to
127 continue. Let’s try it and see:
128
129 ```
130 $ cargo run
131 Compiling panic v0.1.0 (file:///projects/panic)
132 Finished dev [unoptimized + debuginfo] target(s) in 0.27 secs
133 Running `target/debug/panic`
134
135 thread 'main' panicked at 'index out of bounds: the len is 3 but the index is
136 100', /stable-dist-rustc/build/src/libcollections/vec.rs:1362
137 note: Run with `RUST_BACKTRACE=1` for a backtrace.
138 error: Process didn't exit successfully: `target/debug/panic` (exit code: 101)
139 ```
140
141 This error points at a file we didn’t write, *libcollections/vec.rs*. That’s
142 the implementation of `Vec<T>` in the standard library. The code that gets run
143 when we use `[]` on our vector `v` is in *libcollections/vec.rs*, and that is
144 where the `panic!` is actually happening.
145
146 The next note line tells us that we can set the `RUST_BACKTRACE` environment
147 variable to get a backtrace of exactly what happened to cause the error. A
148 *backtrace* is a list of all the functions that have been called to get to this
149 point. Backtraces in Rust work like they do in other languages: the key to
150 reading the backtrace is to start from the top and read until you see files you
151 wrote. That’s the spot where the problem originated. The lines above the lines
152 mentioning your files are code that your code called; the lines below are code
153 that called your code. These lines might include core Rust code, standard
154 library code, or crates that you’re using. Let’s try getting a backtrace:
155 Listing 9-2 shows output similar to what you’ll see:
156
157 ```
158 $ RUST_BACKTRACE=1 cargo run
159 Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
160 Running `target/debug/panic`
161 thread 'main' panicked at 'index out of bounds: the len is 3 but the index is
162 100', /checkout/src/liballoc/vec.rs:1555:10
163 stack backtrace:
164 0: std::sys::imp::backtrace::tracing::imp::unwind_backtrace
165 at /checkout/src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
166 1: std::sys_common::backtrace::_print
167 at /checkout/src/libstd/sys_common/backtrace.rs:71
168 2: std::panicking::default_hook::{{closure}}
169 at /checkout/src/libstd/sys_common/backtrace.rs:60
170 at /checkout/src/libstd/panicking.rs:381
171 3: std::panicking::default_hook
172 at /checkout/src/libstd/panicking.rs:397
173 4: std::panicking::rust_panic_with_hook
174 at /checkout/src/libstd/panicking.rs:611
175 5: std::panicking::begin_panic
176 at /checkout/src/libstd/panicking.rs:572
177 6: std::panicking::begin_panic_fmt
178 at /checkout/src/libstd/panicking.rs:522
179 7: rust_begin_unwind
180 at /checkout/src/libstd/panicking.rs:498
181 8: core::panicking::panic_fmt
182 at /checkout/src/libcore/panicking.rs:71
183 9: core::panicking::panic_bounds_check
184 at /checkout/src/libcore/panicking.rs:58
185 10: <alloc::vec::Vec<T> as core::ops::index::Index<usize>>::index
186 at /checkout/src/liballoc/vec.rs:1555
187 11: panic::main
188 at ./src/main.rs:4
189 12: __rust_maybe_catch_panic
190 at /checkout/src/libpanic_unwind/lib.rs:99
191 13: std::rt::lang_start
192 at /checkout/src/libstd/panicking.rs:459
193 at /checkout/src/libstd/panic.rs:361
194 at /checkout/src/libstd/rt.rs:61
195 14: main
196 15: __libc_start_main
197 16: <unknown>
198 ```
199
200 Listing 9-2: The backtrace generated by a call to `panic!` displayed when the
201 environment variable `RUST_BACKTRACE` is set
202
203 That’s a lot of output! The exact output you see might be different depending
204 on your operating system and Rust version. In order to get backtraces with this
205 information, debug symbols must be enabled. Debug symbols are enabled by
206 default when using cargo build or cargo run without the --release flag, as we
207 have here.
208
209 In the output in Listing 9-2, line 11 of the backtrace points to the line in
210 our project that’s causing the problem: *src/main.rs* in line 4. If we don’t
211 want our program to panic, the location pointed to by the first line mentioning
212 a file we wrote is where we should start investigating to figure out how we got
213 to this location with values that caused the panic. In Listing 9-1 where we
214 deliberately wrote code that would panic in order to demonstrate how to use
215 backtraces, the way to fix the panic is to not request an element at index 100
216 from a vector that only contains three items. When your code panics in the
217 future, you’ll need to figure out what action the code is taking with what
218 values that causes the panic and what the code should do instead.
219
220 We’ll come back to `panic!` and when we should and should not use `panic!` to
221 handle error conditions later in the chapter. Next, we’ll look at how to
222 recover from an error using `Result`.
223
224 ## Recoverable Errors with `Result`
225
226 Most errors aren’t serious enough to require the program to stop entirely.
227 Sometimes, when a function fails, it’s for a reason that we can easily
228 interpret and respond to. For example, if we try to open a file and that
229 operation fails because the file doesn’t exist, we might want to create the
230 file instead of terminating the process.
231
232 Recall in Chapter 2 in the “Handling Potential Failure with the `Result` Type”
233 section that the `Result` enum is defined as having two variants, `Ok` and
234 `Err`, as follows:
235
236 ```
237 enum Result<T, E> {
238 Ok(T),
239 Err(E),
240 }
241 ```
242
243 The `T` and `E` are generic type parameters: we’ll discuss generics in more
244 detail in Chapter 10. What you need to know right now is that `T` represents
245 the type of the value that will be returned in a success case within the `Ok`
246 variant, and `E` represents the type of the error that will be returned in a
247 failure case within the `Err` variant. Because `Result` has these generic type
248 parameters, we can use the `Result` type and the functions that the standard
249 library has defined on it in many different situations where the successful
250 value and error value we want to return may differ.
251
252 Let’s call a function that returns a `Result` value because the function could
253 fail: in Listing 9-3 we try to open a file:
254
255 Filename: src/main.rs
256
257 ```
258 use std::fs::File;
259
260 fn main() {
261 let f = File::open("hello.txt");
262 }
263 ```
264
265 Listing 9-3: Opening a file
266
267 How do we know `File::open` returns a `Result`? We could look at the standard
268 library API documentation, or we could ask the compiler! If we give `f` a type
269 annotation of a type that we know the return type of the function is *not* and
270 then we try to compile the code, the compiler will tell us that the types don’t
271 match. The error message will then tell us what the type of `f` *is*. Let’s try
272 it: we know that the return type of `File::open` isn’t of type `u32`, so let’s
273 change the `let f` statement to this:
274
275 ```
276 let f: u32 = File::open("hello.txt");
277 ```
278
279 Attempting to compile now gives us the following output:
280
281 ```
282 error[E0308]: mismatched types
283 --> src/main.rs:4:18
284 |
285 4 | let f: u32 = File::open("hello.txt");
286 | ^^^^^^^^^^^^^^^^^^^^^^^ expected u32, found enum
287 `std::result::Result`
288 |
289 = note: expected type `u32`
290 = note: found type `std::result::Result<std::fs::File, std::io::Error>`
291 ```
292
293 This tells us the return type of the `File::open` function is a `Result<T, E>`.
294 The generic parameter `T` has been filled in here with the type of the success
295 value, `std::fs::File`, which is a file handle. The type of `E` used in the
296 error value is `std::io::Error`.
297
298 This return type means the call to `File::open` might succeed and return to us
299 a file handle that we can read from or write to. The function call also might
300 fail: for example, the file might not exist or we might not have permission to
301 access the file. The `File::open` function needs to have a way to tell us
302 whether it succeeded or failed and at the same time give us either the file
303 handle or error information. This information is exactly what the `Result` enum
304 conveys.
305
306 In the case where `File::open` succeeds, the value we will have in the variable
307 `f` will be an instance of `Ok` that contains a file handle. In the case where
308 it fails, the value in `f` will be an instance of `Err` that contains more
309 information about the kind of error that happened.
310
311 We need to add to the code in Listing 9-3 to take different actions depending
312 on the value `File::open` returned. Listing 9-4 shows one way to handle the
313 `Result` using a basic tool: the `match` expression that we discussed in
314 Chapter 6.
315
316 Filename: src/main.rs
317
318 ```
319 use std::fs::File;
320
321 fn main() {
322 let f = File::open("hello.txt");
323
324 let f = match f {
325 Ok(file) => file,
326 Err(error) => {
327 panic!("There was a problem opening the file: {:?}", error)
328 },
329 };
330 }
331 ```
332
333 Listing 9-4: Using a `match` expression to handle the `Result` variants we
334 might have
335
336 Note that, like the `Option` enum, the `Result` enum and its variants have been
337 imported in the prelude, so we don’t need to specify `Result::` before the `Ok`
338 and `Err` variants in the `match` arms.
339
340 Here we tell Rust that when the result is `Ok`, return the inner `file` value
341 out of the `Ok` variant, and we then assign that file handle value to the
342 variable `f`. After the `match`, we can then use the file handle for reading or
343 writing.
344
345 The other arm of the `match` handles the case where we get an `Err` value from
346 `File::open`. In this example, we’ve chosen to call the `panic!` macro. If
347 there’s no file named *hello.txt* in our current directory and we run this
348 code, we’ll see the following output from the `panic!` macro:
349
350 ```
351 thread 'main' panicked at 'There was a problem opening the file: Error { repr:
352 Os { code: 2, message: "No such file or directory" } }', src/main.rs:8
353 ```
354
355 As usual, this output tells us exactly what has gone wrong.
356
357 ### Matching on Different Errors
358
359 The code in Listing 9-4 will `panic!` no matter the reason that `File::open`
360 failed. What we want to do instead is take different actions for different
361 failure reasons: if `File::open` failed because the file doesn’t exist, we want
362 to create the file and return the handle to the new file. If `File::open`
363 failed for any other reason, for example because we didn’t have permission to
364 open the file, we still want the code to `panic!` in the same way as it did in
365 Listing 9-4. Look at Listing 9-5, which adds another arm to the `match`:
366
367 Filename: src/main.rs
368
369 ```
370 use std::fs::File;
371 use std::io::ErrorKind;
372
373 fn main() {
374 let f = File::open("hello.txt");
375
376 let f = match f {
377 Ok(file) => file,
378 Err(ref error) if error.kind() == ErrorKind::NotFound => {
379 match File::create("hello.txt") {
380 Ok(fc) => fc,
381 Err(e) => {
382 panic!(
383 "Tried to create file but there was a problem: {:?}",
384 e
385 )
386 },
387 }
388 },
389 Err(error) => {
390 panic!(
391 "There was a problem opening the file: {:?}",
392 error
393 )
394 },
395 };
396 }
397 ```
398
399 Listing 9-5: Handling different kinds of errors in different ways
400
401 The type of the value that `File::open` returns inside the `Err` variant is
402 `io::Error`, which is a struct provided by the standard library. This struct
403 has a method `kind` that we can call to get an `io::ErrorKind` value.
404 `io::ErrorKind` is an enum provided by the standard library that has variants
405 representing the different kinds of errors that might result from an `io`
406 operation. The variant we want to use is `ErrorKind::NotFound`, which indicates
407 the file we’re trying to open doesn’t exist yet.
408
409 The condition `if error.kind() == ErrorKind::NotFound` is called a *match
410 guard*: it’s an extra condition on a `match` arm that further refines the arm’s
411 pattern. This condition must be true for that arm’s code to be run; otherwise,
412 the pattern matching will move on to consider the next arm in the `match`. The
413 `ref` in the pattern is needed so `error` is not moved into the guard condition
414 but is merely referenced by it. The reason `ref` is used to take a reference in
415 a pattern instead of `&` will be covered in detail in Chapter 18. In short, in
416 the context of a pattern, `&` matches a reference and gives us its value, but
417 `ref` matches a value and gives us a reference to it.
418
419 The condition we want to check in the match guard is whether the value returned
420 by `error.kind()` is the `NotFound` variant of the `ErrorKind` enum. If it is,
421 we try to create the file with `File::create`. However, because `File::create`
422 could also fail, we need to add an inner `match` statement as well. When the
423 file can’t be opened, a different error message will be printed. The last arm
424 of the outer `match` stays the same so the program panics on any error besides
425 the missing file error.
426
427 ### Shortcuts for Panic on Error: `unwrap` and `expect`
428
429 Using `match` works well enough, but it can be a bit verbose and doesn’t always
430 communicate intent well. The `Result<T, E>` type has many helper methods
431 defined on it to do various tasks. One of those methods, called `unwrap`, is a
432 shortcut method that is implemented just like the `match` statement we wrote in
433 Listing 9-4. If the `Result` value is the `Ok` variant, `unwrap` will return
434 the value inside the `Ok`. If the `Result` is the `Err` variant, `unwrap` will
435 call the `panic!` macro for us. Here is an example of `unwrap` in action:
436
437 Filename: src/main.rs
438
439 ```
440 use std::fs::File;
441
442 fn main() {
443 let f = File::open("hello.txt").unwrap();
444 }
445 ```
446
447 If we run this code without a *hello.txt* file, we’ll see an error message from
448 the `panic!` call that the `unwrap` method makes:
449
450 ```
451 thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error {
452 repr: Os { code: 2, message: "No such file or directory" } }',
453 /stable-dist-rustc/build/src/libcore/result.rs:868
454 ```
455
456 Another method, `expect`, which is similar to `unwrap`, lets us also choose the
457 `panic!` error message. Using `expect` instead of `unwrap` and providing good
458 error messages can convey your intent and make tracking down the source of a
459 panic easier. The syntax of `expect` looks like this:
460
461 Filename: src/main.rs
462
463 ```
464 use std::fs::File;
465
466 fn main() {
467 let f = File::open("hello.txt").expect("Failed to open hello.txt");
468 }
469 ```
470
471 We use `expect` in the same way as `unwrap`: to return the file handle or call
472 the `panic!` macro. The error message used by `expect` in its call to `panic!`
473 will be the parameter that we pass to `expect`, rather than the default
474 `panic!` message that `unwrap` uses. Here’s what it looks like:
475
476 ```
477 thread 'main' panicked at 'Failed to open hello.txt: Error { repr: Os { code:
478 2, message: "No such file or directory" } }',
479 /stable-dist-rustc/build/src/libcore/result.rs:868
480 ```
481
482 Because this error message starts with the text we specified, `Failed to open
483 hello.txt`, it will be easier to find where in the code this error message is
484 coming from. If we use `unwrap` in multiple places, it can take more time to
485 figure out exactly which `unwrap` is causing the panic because all `unwrap`
486 calls that panic print the same message.
487
488 ### Propagating Errors
489
490 When you’re writing a function whose implementation calls something that might
491 fail, instead of handling the error within this function, you can return the
492 error to the calling code so that it can decide what to do. This is known as
493 *propagating* the error and gives more control to the calling code where there
494 might be more information or logic that dictates how the error should be
495 handled than what you have available in the context of your code.
496
497 For example, Listing 9-6 shows a function that reads a username from a file. If
498 the file doesn’t exist or can’t be read, this function will return those errors
499 to the code that called this function:
500
501 Filename: src/main.rs
502
503 ```
504 use std::io;
505 use std::io::Read;
506 use std::fs::File;
507
508 fn read_username_from_file() -> Result<String, io::Error> {
509 let f = File::open("hello.txt");
510
511 let mut f = match f {
512 Ok(file) => file,
513 Err(e) => return Err(e),
514 };
515
516 let mut s = String::new();
517
518 match f.read_to_string(&mut s) {
519 Ok(_) => Ok(s),
520 Err(e) => Err(e),
521 }
522 }
523 ```
524
525 Listing 9-6: A function that returns errors to the calling code using `match`
526
527 Let’s look at the return type of the function first: `Result<String,
528 io::Error>`. This means the function is returning a value of the type
529 `Result<T, E>` where the generic parameter `T` has been filled in with the
530 concrete type `String`, and the generic type `E` has been filled in with the
531 concrete type `io::Error`. If this function succeeds without any problems, the
532 code that calls this function will receive an `Ok` value that holds a
533 `String`—the username that this function read from the file. If this function
534 encounters any problems, the code that calls this function will receive an
535 `Err` value that holds an instance of `io::Error` that contains more
536 information about what the problems were. We chose `io::Error` as the return
537 type of this function because that happens to be the type of the error value
538 returned from both of the operations we’re calling in this function’s body that
539 might fail: the `File::open` function and the `read_to_string` method.
540
541 The body of the function starts by calling the `File::open` function. Then we
542 handle the `Result` value returned with a `match` similar to the `match` in
543 Listing 9-4, only instead of calling `panic!` in the `Err` case, we return
544 early from this function and pass the error value from `File::open` back to the
545 calling code as this function’s error value. If `File::open` succeeds, we store
546 the file handle in the variable `f` and continue.
547
548 Then we create a new `String` in variable `s` and call the `read_to_string`
549 method on the file handle in `f` to read the contents of the file into `s`. The
550 `read_to_string` method also returns a `Result` because it might fail, even
551 though `File::open` succeeded. So we need another `match` to handle that
552 `Result`: if `read_to_string` succeeds, then our function has succeeded, and we
553 return the username from the file that’s now in `s` wrapped in an `Ok`. If
554 `read_to_string` fails, we return the error value in the same way that we
555 returned the error value in the `match` that handled the return value of
556 `File::open`. However, we don’t need to explicitly say `return`, because this
557 is the last expression in the function.
558
559 The code that calls this code will then handle getting either an `Ok` value
560 that contains a username or an `Err` value that contains an `io::Error`. We
561 don’t know what the calling code will do with those values. If the calling code
562 gets an `Err` value, it could call `panic!` and crash the program, use a
563 default username, or look up the username from somewhere other than a file, for
564 example. We don’t have enough information on what the calling code is actually
565 trying to do, so we propagate all the success or error information upwards for
566 it to handle appropriately.
567
568 This pattern of propagating errors is so common in Rust that Rust provides the
569 question mark operator `?` to make this easier.
570
571 #### A Shortcut for Propagating Errors: `?`
572
573 Listing 9-7 shows an implementation of `read_username_from_file` that has the
574 same functionality as it had in Listing 9-6, but this implementation uses the
575 question mark operator:
576
577 Filename: src/main.rs
578
579 ```
580 use std::io;
581 use std::io::Read;
582 use std::fs::File;
583
584 fn read_username_from_file() -> Result<String, io::Error> {
585 let mut f = File::open("hello.txt")?;
586 let mut s = String::new();
587 f.read_to_string(&mut s)?;
588 Ok(s)
589 }
590 ```
591
592 Listing 9-7: A function that returns errors to the calling code using `?`
593
594 The `?` placed after a `Result` value is defined to work in almost the same way
595 as the `match` expressions we defined to handle the `Result` values in Listing
596 9-6. If the value of the `Result` is an `Ok`, the value inside the `Ok` will
597 get returned from this expression and the program will continue. If the value
598 is an `Err`, the value inside the `Err` will be returned from the whole
599 function as if we had used the `return` keyword so the error value gets
600 propagated to the calling code.
601
602 The one difference between the `match` expression from Listing 9-6 and what the
603 question mark operator does is that when using the question mark operator,
604 error values go through the `from` function defined in the `From` trait in the
605 standard library. Many error types implement the `from` function to convert an
606 error of one type into an error of another type. When used by the question mark
607 operator, the call to the `from` function converts the error type that the
608 question mark operator gets into the error type defined in the return type of
609 the current function that we’re using `?` in. This is useful when parts of a
610 function might fail for many different reasons, but the function returns one
611 error type that represents all the ways the function might fail. As long as
612 each error type implements the `from` function to define how to convert itself
613 to the returned error type, the question mark operator takes care of the
614 conversion automatically.
615
616 In the context of Listing 9-7, the `?` at the end of the `File::open` call will
617 return the value inside an `Ok` to the variable `f`. If an error occurs, `?`
618 will return early out of the whole function and give any `Err` value to the
619 calling code. The same thing applies to the `?` at the end of the
620 `read_to_string` call.
621
622 The `?` eliminates a lot of boilerplate and makes this function’s
623 implementation simpler. We could even shorten this code further by chaining
624 method calls immediately after the `?` as shown in Listing 9-8:
625
626 Filename: src/main.rs
627
628 ```
629 use std::io;
630 use std::io::Read;
631 use std::fs::File;
632
633 fn read_username_from_file() -> Result<String, io::Error> {
634 let mut s = String::new();
635
636 File::open("hello.txt")?.read_to_string(&mut s)?;
637 Ok(s)
638 }
639 ```
640
641 Listing 9-8: Chaining method calls after the question mark operator
642
643 We’ve moved the creation of the new `String` in `s` to the beginning of the
644 function; that part hasn’t changed. Instead of creating a variable `f`, we’ve
645 chained the call to `read_to_string` directly onto the result of
646 `File::open("hello.txt")?`. We still have a `?` at the end of the
647 `read_to_string` call, and we still return an `Ok` value containing the
648 username in `s` when both `File::open` and `read_to_string` succeed rather than
649 returning errors. The functionality is again the same as in Listing 9-6 and
650 Listing 9-7; this is just a different, more ergonomic way to write it.
651
652 #### `?` Can Only Be Used in Functions That Return Result
653
654 The `?` can only be used in functions that have a return type of `Result`,
655 because it is defined to work in the same way as the `match` expression we
656 defined in Listing 9-6. The part of the `match` that requires a return type of
657 `Result` is `return Err(e)`, so the return type of the function must be a
658 `Result` to be compatible with this `return`.
659
660 Let’s look at what happens if we use `?` in the `main` function, which you’ll
661 recall has a return type of `()`:
662
663 ```
664 use std::fs::File;
665
666 fn main() {
667 let f = File::open("hello.txt")?;
668 }
669 ```
670
671 When we compile this code, we get the following error message:
672
673 ```
674 error[E0277]: the `?` operator can only be used in a function that returns
675 `Result` (or another type that implements `std::ops::Try`)
676 --> src/main.rs:4:13
677 |
678 4 | let f = File::open("hello.txt")?;
679 | ------------------------
680 | |
681 | cannot use the `?` operator in a function that returns `()`
682 | in this macro invocation
683 |
684 = help: the trait `std::ops::Try` is not implemented for `()`
685 = note: required by `std::ops::Try::from_error`
686 ```
687
688 This error points out that we’re only allowed to use the question mark operator
689 in a function that returns `Result`. In functions that don’t return `Result`,
690 when you call other functions that return `Result`, you’ll need to use a
691 `match` or one of the `Result` methods to handle it instead of using `?` to
692 potentially propagate the error to the calling code.
693
694 Now that we’ve discussed the details of calling `panic!` or returning `Result`,
695 let’s return to the topic of how to decide which is appropriate to use in which
696 cases.
697
698 ## To `panic!` or Not to `panic!`
699
700 So how do you decide when you should `panic!` and when you should return
701 `Result`? When code panics, there’s no way to recover. You could call `panic!`
702 for any error situation, whether there’s a possible way to recover or not, but
703 then you’re making the decision on behalf of the code calling your code that a
704 situation is unrecoverable. When you choose to return a `Result` value, you
705 give the calling code options rather than making the decision for it. The
706 calling code could choose to attempt to recover in a way that’s appropriate for
707 its situation, or it could decide that an `Err` value in this case is
708 unrecoverable, so it can call `panic!` and turn your recoverable error into an
709 unrecoverable one. Therefore, returning `Result` is a good default choice when
710 you’re defining a function that might fail.
711
712 In a few situations it’s more appropriate to write code that panics instead of
713 returning a `Result`, but they are less common. Let’s explore why it’s
714 appropriate to panic in examples, prototype code, and tests; then in situations
715 where you as a human can know a method won’t fail that the compiler can’t
716 reason about; and conclude with some general guidelines on how to decide
717 whether to panic in library code.
718
719 ### Examples, Prototype Code, and Tests Are All Places it’s Perfectly Fine to Panic
720
721 When you’re writing an example to illustrate some concept, having robust error
722 handling code in the example as well can make the example less clear. In
723 examples, it’s understood that a call to a method like `unwrap` that could
724 `panic!` is meant as a placeholder for the way that you’d want your application
725 to handle errors, which can differ based on what the rest of your code is doing.
726
727 Similarly, the `unwrap` and `expect` methods are very handy when prototyping,
728 before you’re ready to decide how to handle errors. They leave clear markers in
729 your code for when you’re ready to make your program more robust.
730
731 If a method call fails in a test, we’d want the whole test to fail, even if
732 that method isn’t the functionality under test. Because `panic!` is how a test
733 is marked as a failure, calling `unwrap` or `expect` is exactly what should
734 happen.
735
736 ### Cases When You Have More Information Than the Compiler
737
738 It would also be appropriate to call `unwrap` when you have some other logic
739 that ensures the `Result` will have an `Ok` value, but the logic isn’t
740 something the compiler understands. You’ll still have a `Result` value that you
741 need to handle: whatever operation you’re calling still has the possibility of
742 failing in general, even though it’s logically impossible in your particular
743 situation. If you can ensure by manually inspecting the code that you’ll never
744 have an `Err` variant, it’s perfectly acceptable to call `unwrap`. Here’s an
745 example:
746
747 ```
748 use std::net::IpAddr;
749
750 let home = "127.0.0.1".parse::<IpAddr>().unwrap();
751 ```
752
753 We’re creating an `IpAddr` instance by parsing a hardcoded string. We can see
754 that `127.0.0.1` is a valid IP address, so it’s acceptable to use `unwrap`
755 here. However, having a hardcoded, valid string doesn’t change the return type
756 of the `parse` method: we still get a `Result` value, and the compiler will
757 still make us handle the `Result` as if the `Err` variant is still a
758 possibility because the compiler isn’t smart enough to see that this string is
759 always a valid IP address. If the IP address string came from a user rather
760 than being hardcoded into the program, and therefore *did* have a possibility
761 of failure, we’d definitely want to handle the `Result` in a more robust way
762 instead.
763
764 ### Guidelines for Error Handling
765
766 It’s advisable to have your code `panic!` when it’s possible that your code
767 could end up in a bad state. In this context, bad state is when some
768 assumption, guarantee, contract, or invariant has been broken, such as when
769 invalid values, contradictory values, or missing values are passed to your
770 code—plus one or more of the following:
771
772 * The bad state is not something that’s *expected* to happen occasionally.
773 * Your code after this point needs to rely on not being in this bad state.
774 * There’s not a good way to encode this information in the types you use.
775
776 If someone calls your code and passes in values that don’t make sense, the best
777 choice might be to `panic!` and alert the person using your library to the bug
778 in their code so they can fix it during development. Similarly, `panic!` is
779 often appropriate if you’re calling external code that is out of your control,
780 and it returns an invalid state that you have no way of fixing.
781
782 When a bad state is reached, but it’s expected to happen no matter how well you
783 write your code, it’s still more appropriate to return a `Result` rather than
784 making a `panic!` call. Examples of this include a parser being given malformed
785 data or an HTTP request returning a status that indicates you have hit a rate
786 limit. In these cases, you should indicate that failure is an expected
787 possibility by returning a `Result` to propagate these bad states upwards so
788 the calling code can decide how to handle the problem. To `panic!` wouldn’t be
789 the best way to handle these cases.
790
791 When your code performs operations on values, your code should verify the
792 values are valid first, and `panic!` if the values aren’t valid. This is mostly
793 for safety reasons: attempting to operate on invalid data can expose your code
794 to vulnerabilities. This is the main reason the standard library will `panic!`
795 if you attempt an out-of-bounds memory access: trying to access memory that
796 doesn’t belong to the current data structure is a common security problem.
797 Functions often have *contracts*: their behavior is only guaranteed if the
798 inputs meet particular requirements. Panicking when the contract is violated
799 makes sense because a contract violation always indicates a caller-side bug,
800 and it’s not a kind of error you want the calling code to have to explicitly
801 handle. In fact, there’s no reasonable way for calling code to recover: the
802 calling *programmers* need to fix the code. Contracts for a function,
803 especially when a violation will cause a panic, should be explained in the API
804 documentation for the function.
805
806 However, having lots of error checks in all of your functions would be verbose
807 and annoying. Fortunately, you can use Rust’s type system (and thus the type
808 checking the compiler does) to do many of the checks for you. If your function
809 has a particular type as a parameter, you can proceed with your code’s logic
810 knowing that the compiler has already ensured you have a valid value. For
811 example, if you have a type rather than an `Option`, your program expects to
812 have *something* rather than *nothing*. Your code then doesn’t have to handle
813 two cases for the `Some` and `None` variants: it will only have one case for
814 definitely having a value. Code trying to pass nothing to your function won’t
815 even compile, so your function doesn’t have to check for that case at runtime.
816 Another example is using an unsigned integer type like `u32`, which ensures the
817 parameter is never negative.
818
819 ### Creating Custom Types for Validation
820
821 Let’s take the idea of using Rust’s type system to ensure we have a valid value
822 one step further and look at creating a custom type for validation. Recall the
823 guessing game in Chapter 2 where our code asked the user to guess a number
824 between 1 and 100. We never validated that the user’s guess was between those
825 numbers before checking it against our secret number; we only validated that
826 the guess was positive. In this case, the consequences were not very dire: our
827 output of “Too high” or “Too low” would still be correct. It would be a useful
828 enhancement to guide the user toward valid guesses and have different behavior
829 when a user guesses a number that’s out of range versus when a user types, for
830 example, letters instead.
831
832 One way to do this would be to parse the guess as an `i32` instead of only a
833 `u32` to allow potentially negative numbers, and then add a check for the
834 number being in range, like so:
835
836 ```
837 loop {
838 // snip
839
840 let guess: i32 = match guess.trim().parse() {
841 Ok(num) => num,
842 Err(_) => continue,
843 };
844
845 if guess < 1 || guess > 100 {
846 println!("The secret number will be between 1 and 100.");
847 continue;
848 }
849
850 match guess.cmp(&secret_number) {
851 // snip
852 }
853 ```
854
855 The `if` expression checks whether our value is out of range, tells the user
856 about the problem, and calls `continue` to start the next iteration of the loop
857 and ask for another guess. After the `if` expression, we can proceed with the
858 comparisons between `guess` and the secret number knowing that `guess` is
859 between 1 and 100.
860
861 However, this is not an ideal solution: if it was absolutely critical that the
862 program only operated on values between 1 and 100, and it had many functions
863 with this requirement, it would be tedious (and potentially impact performance)
864 to have a check like this in every function.
865
866 Instead, we can make a new type and put the validations in a function to create
867 an instance of the type rather than repeating the validations everywhere. That
868 way, it’s safe for functions to use the new type in their signatures and
869 confidently use the values they receive. Listing 9-9 shows one way to define a
870 `Guess` type that will only create an instance of `Guess` if the `new` function
871 receives a value between 1 and 100:
872
873 ```
874 pub struct Guess {
875 value: u32,
876 }
877
878 impl Guess {
879 pub fn new(value: u32) -> Guess {
880 if value < 1 || value > 100 {
881 panic!("Guess value must be between 1 and 100, got {}.", value);
882 }
883
884 Guess {
885 value
886 }
887 }
888
889 pub fn value(&self) -> u32 {
890 self.value
891 }
892 }
893 ```
894
895 Listing 9-9: A `Guess` type that will only continue with values between 1 and
896 100
897
898 First, we define a struct named `Guess` that has a field named `value` that
899 holds a `u32`. This is where the number will be stored.
900
901 Then we implement an associated function named `new` on `Guess` that creates
902 instances of `Guess` values. The `new` function is defined to have one
903 parameter named `value` of type `u32` and to return a `Guess`. The code in the
904 body of the `new` function tests `value` to make sure it’s between 1 and 100.
905 If `value` doesn’t pass this test, we make a `panic!` call, which will alert
906 the programmer who is writing the calling code that they have a bug they need
907 to fix, because creating a `Guess` with a `value` outside this range would
908 violate the contract that `Guess::new` is relying on. The conditions in which
909 `Guess::new` might panic should be discussed in its public-facing API
910 documentation; we’ll cover documentation conventions indicating the possibility
911 of a `panic!` in the API documentation that you create in Chapter 14. If
912 `value` does pass the test, we create a new `Guess` with its `value` field set
913 to the `value` parameter and return the `Guess`.
914
915 Next, we implement a method named `value` that borrows `self`, doesn’t have any
916 other parameters, and returns a `u32`. This is a kind of method sometimes
917 called a *getter*, because its purpose is to get some data from its fields and
918 return it. This public method is necessary because the `value` field of the
919 `Guess` struct is private. It’s important that the `value` field is private so
920 code using the `Guess` struct is not allowed to set `value` directly: code
921 outside the module *must* use the `Guess::new` function to create an instance
922 of `Guess`, which ensures there’s no way for a `Guess` to have a `value` that
923 hasn’t been checked by the conditions in the `Guess::new` function.
924
925 A function that has a parameter or returns only numbers between 1 and 100 could
926 then declare in its signature that it takes or returns a `Guess` rather than a
927 `u32` and wouldn’t need to do any additional checks in its body.
928
929 ## Summary
930
931 Rust’s error handling features are designed to help you write more robust code.
932 The `panic!` macro signals that your program is in a state it can’t handle and
933 lets you tell the process to stop instead of trying to proceed with invalid or
934 incorrect values. The `Result` enum uses Rust’s type system to indicate that
935 operations might fail in a way that your code could recover from. You can use
936 `Result` to tell code that calls your code that it needs to handle potential
937 success or failure as well. Using `panic!` and `Result` in the appropriate
938 situations will make your code more reliable in the face of inevitable problems.
939
940 Now that you’ve seen useful ways that the standard library uses generics with
941 the `Option` and `Result` enums, we’ll talk about how generics work and how you
942 can use them in your code in the next chapter.
943