Rust by Example中文

16 Error handling

Error handling is the process of handling the possibility of failure. For example, failing to read a file and then continuing to use that bad input would clearly be problematic. Error handling allows us to notice and handle those errors in an explicit fashion, saving the rest of the program from potential issues.

The simplest error handling mechanism we will see is panic. It prints an error message, starts unwinding the task, and usually exits the program. Consider the following example:

fn give_princess(gift: &str) { // Princesses hate snakes, so we need to stop if // she expresses her disapproval! if gift == "snake" { panic!("AAAaaaaa!!!!"); } println!("I love {}s!!!!!", gift); } fn main() { give_princess("teddy bear"); give_princess("snake"); }

This shows that we can induce program failure at will, but raises a question: what happens if the princess is not given a gift? We could explicitly test this with a check against the null string ("") as we do with the snake, but this is not reliable. The problem is that programmers do not habitually test these checks unless required to by the compiler.

In order for this to be reliable, we'll want the compiler to point out cases where there may not be a gift. As you'll see in the next section, we can use Option to take care of this condition.