Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace panic example with a simpler version #1380

Merged
merged 1 commit into from
Sep 28, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/error/panic.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# `panic`

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

```rust,editable,ignore,mdbook-runnable
fn give_princess(gift: &str) {
// Princesses hate snakes, so we need to stop if she disapproves!
if gift == "snake" { panic!("AAAaaaaa!!!!"); }
fn drink(beverage: &str) {
// You shouldn't drink too much sugary beverages.
if beverage == "lemonade" { panic!("AAAaaaaa!!!!"); }

println!("I love {}s!!!!!", gift);
println!("Some refreshing {} is all I need.", beverage);
}

fn main() {
give_princess("teddy bear");
give_princess("snake");
drink("water");
drink("lemonade");
}
```