diff --git a/src/SUMMARY.md b/src/SUMMARY.md index c3875079c3..6ec3d8bffa 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -155,6 +155,7 @@ - [Error handling](error.md) - [`panic`](error/panic.md) + - [`abort` & `unwind`](error/abort_unwind.md) - [`Option` & `unwrap`](error/option_unwrap.md) - [Unpacking options with `?`](error/option_unwrap/question_mark.md) - [Combinators: `map`](error/option_unwrap/map.md) diff --git a/src/error/abort_unwind.md b/src/error/abort_unwind.md new file mode 100644 index 0000000000..346a619ecd --- /dev/null +++ b/src/error/abort_unwind.md @@ -0,0 +1,51 @@ +# `abort` and `unwind` + +The previous section illustrates the error handling mechanism `panic`. The `cfg_panic` feature makes it possible to execute different code depending on the panic strategy. The current values available are `unwind` and `abort`. + + +Building on the prior lemonade example, we explicitly use the panic strategy to execise different lines of code. + +```rust,editable,ignore,mdbook-runnable + +fn drink(beverage: &str) { + // You shouldn't drink too much sugary beverages. + if beverage == "lemonade" { + if cfg!(panic="abort"){ println!("This is not your party. Run!!!!");} + else{ println!("Spit it out!!!!");} + } + else{ println!("Some refreshing {} is all I need.", beverage); } +} + +fn main() { + drink("water"); + drink("lemonade"); +} +``` + +Here is another example focusing on rewriting `drink()` and explicitly use the `unwind` keyword. + +```rust,editable,ignore + +#[cfg(panic = "unwind")] +fn ah(){ println!("Spit it out!!!!");} + +#[cfg(not(panic="unwind"))] +fn ah(){ println!("This is not your party. Run!!!!");} + +fn drink(beverage: &str){ + if beverage == "lemonade"{ ah();} + else{println!("Some refreshing {} is all I need.", beverage);} +} + +fn main() { + drink("water"); + drink("lemonade"); +} +``` + +The panic strategy can be set from the command line by using `abort` or `unwind`. + +```rust,editable,ignore +rustc lemonade.rc -C panic=abort +``` +