panic

The simplest error handling mechanism we will see is panic. It prints an
error message, starts unwinding the task, and usually exits the program.
Here, we explicitly call panic on our error condition:

  1. fn give_princess(gift: &str) {
  2. // Princesses hate snakes, so we need to stop if she disapproves!
  3. if gift == "snake" { panic!("AAAaaaaa!!!!"); }
  4. println!("I love {}s!!!!!", gift);
  5. }
  6. fn main() {
  7. give_princess("teddy bear");
  8. give_princess("snake");
  9. }