3 > The best-laid plans of mice and men
6 > "Tae a Moose", Robert Burns
8 Sometimes, things just go wrong. It's important to have a plan for when the
9 inevitable happens. Rust has rich support for handling errors that may (let's
10 be honest: will) occur in your programs.
12 There are two main kinds of errors that can occur in your programs: failures,
13 and panics. Let's talk about the difference between the two, and then discuss
14 how to handle each. Then, we'll discuss upgrading failures to panics.
18 Rust uses two terms to differentiate between two forms of error: failure, and
19 panic. A *failure* is an error that can be recovered from in some way. A
20 *panic* is an error that cannot be recovered from.
22 What do we mean by "recover"? Well, in most cases, the possibility of an error
23 is expected. For example, consider the `parse` function:
29 This method converts a string into another type. But because it's a string, you
30 can't be sure that the conversion actually works. For example, what should this
34 "hello5world".parse();
37 This won't work. So we know that this function will only work properly for some
38 inputs. It's expected behavior. We call this kind of error a *failure*.
40 On the other hand, sometimes, there are errors that are unexpected, or which
41 we cannot recover from. A classic example is an `assert!`:
48 We use `assert!` to declare that something is true. If it's not true, something
49 is very wrong. Wrong enough that we can't continue with things in the current
50 state. Another example is using the `unreachable!()` macro:
57 fn probability(_: &Event) -> f64 {
58 // real implementation would be more complex, of course
62 fn descriptive_probability(event: Event) -> &'static str {
63 match probability(&event) {
66 0.00 ... 0.25 => "very unlikely",
67 0.25 ... 0.50 => "unlikely",
68 0.50 ... 0.75 => "likely",
69 0.75 ... 1.00 => "very likely",
74 std::io::println(descriptive_probability(NewRelease));
78 This will give us an error:
81 error: non-exhaustive patterns: `_` not covered [E0004]
84 While we know that we've covered all possible cases, Rust can't tell. It
85 doesn't know that probability is between 0.0 and 1.0. So we add another case:
88 use Event::NewRelease;
94 fn probability(_: &Event) -> f64 {
95 // real implementation would be more complex, of course
99 fn descriptive_probability(event: Event) -> &'static str {
100 match probability(&event) {
102 0.00 => "impossible",
103 0.00 ... 0.25 => "very unlikely",
104 0.25 ... 0.50 => "unlikely",
105 0.50 ... 0.75 => "likely",
106 0.75 ... 1.00 => "very likely",
112 println!("{}", descriptive_probability(NewRelease));
116 We shouldn't ever hit the `_` case, so we use the `unreachable!()` macro to
117 indicate this. `unreachable!()` gives a different kind of error than `Result`.
118 Rust calls these sorts of errors *panics*.
120 # Handling errors with `Option` and `Result`
122 The simplest way to indicate that a function may fail is to use the `Option<T>`
123 type. For example, the `find` method on strings attempts to find a pattern
124 in a string, and returns an `Option`:
129 assert_eq!(s.find('f'), Some(0));
130 assert_eq!(s.find('z'), None);
134 This is appropriate for the simplest of cases, but doesn't give us a lot of
135 information in the failure case. What if we wanted to know _why_ the function
136 failed? For this, we can use the `Result<T, E>` type. It looks like this:
145 This enum is provided by Rust itself, so you don't need to define it to use it
146 in your code. The `Ok(T)` variant represents a success, and the `Err(E)` variant
147 represents a failure. Returning a `Result` instead of an `Option` is recommended
148 for all but the most trivial of situations.
150 Here's an example of using `Result`:
154 enum Version { Version1, Version2 }
157 enum ParseError { InvalidHeaderLength, InvalidVersion }
159 fn parse_version(header: &[u8]) -> Result<Version, ParseError> {
160 if header.len() < 1 {
161 return Err(ParseError::InvalidHeaderLength);
164 1 => Ok(Version::Version1),
165 2 => Ok(Version::Version2),
166 _ => Err(ParseError::InvalidVersion)
170 let version = parse_version(&[1, 2, 3, 4]);
173 println!("working with version: {:?}", v);
176 println!("error parsing header: {:?}", e);
181 This function makes use of an enum, `ParseError`, to enumerate the various
182 errors that can occur.
184 The [`Debug`](../std/fmt/trait.Debug.html) trait is what lets us print the enum value using the `{:?}` format operation.
186 # Non-recoverable errors with `panic!`
188 In the case of an error that is unexpected and not recoverable, the `panic!`
189 macro will induce a panic. This will crash the current thread, and give an error:
198 thread '<main>' panicked at 'boom', hello.rs:2
203 Because these kinds of situations are relatively rare, use panics sparingly.
205 # Upgrading failures to panics
207 In certain circumstances, even though a function may fail, we may want to treat
208 it as a panic instead. For example, `io::stdin().read_line(&mut buffer)` returns
209 a `Result<usize>`, when there is an error reading the line. This allows us to
210 handle and possibly recover from error.
212 If we don't want to handle this error, and would rather just abort the program,
213 we can use the `unwrap()` method:
216 io::stdin().read_line(&mut buffer).unwrap();
219 `unwrap()` will `panic!` if the `Result` is `Err`. This basically says "Give
220 me the value, and if something goes wrong, just crash." This is less reliable
221 than matching the error and attempting to recover, but is also significantly
222 shorter. Sometimes, just crashing is appropriate.
224 There's another way of doing this that's a bit nicer than `unwrap()`:
227 let mut buffer = String::new();
228 let input = io::stdin().read_line(&mut buffer)
230 .expect("Failed to read line");
233 `ok()` converts the `Result` into an `Option`, and `expect()` does the same
234 thing as `unwrap()`, but takes a message. This message is passed along to the
235 underlying `panic!`, providing a better error message if the code errors.
239 When writing code that calls many functions that return the `Result` type, the
240 error handling can be tedious. The `try!` macro hides some of the boilerplate
241 of propagating errors up the call stack.
248 use std::io::prelude::*;
256 fn write_info(info: &Info) -> io::Result<()> {
257 let mut file = File::create("my_best_friends.txt").unwrap();
259 if let Err(e) = writeln!(&mut file, "name: {}", info.name) {
262 if let Err(e) = writeln!(&mut file, "age: {}", info.age) {
265 if let Err(e) = writeln!(&mut file, "rating: {}", info.rating) {
278 use std::io::prelude::*;
286 fn write_info(info: &Info) -> io::Result<()> {
287 let mut file = try!(File::create("my_best_friends.txt"));
289 try!(writeln!(&mut file, "name: {}", info.name));
290 try!(writeln!(&mut file, "age: {}", info.age));
291 try!(writeln!(&mut file, "rating: {}", info.rating));
297 Wrapping an expression in `try!` will result in the unwrapped success (`Ok`)
298 value, unless the result is `Err`, in which case `Err` is returned early from
299 the enclosing function.
301 It's worth noting that you can only use `try!` from a function that returns a
302 `Result`, which means that you cannot use `try!` inside of `main()`, because
303 `main()` doesn't return anything.
305 `try!` makes use of [`From<Error>`](../std/convert/trait.From.html) to determine
306 what to return in the error case.