From 2832a6594a62137df3090732685a20df09aea480 Mon Sep 17 00:00:00 2001 From: Charisee Date: Thu, 3 Feb 2022 19:39:54 +0000 Subject: [PATCH 1/3] documenation for abort and unwind --- src/SUMMARY.md | 1 + src/error/abort_unwind.md | 53 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 src/error/abort_unwind.md 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..4a845b4ee7 --- /dev/null +++ b/src/error/abort_unwind.md @@ -0,0 +1,53 @@ +# `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 +#![feature(cfg_panic)] + +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 +#![feature(cfg_panic)] + +#[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 +``` + From 51a361df83d18ac06488f6d0be65e9233018e52f Mon Sep 17 00:00:00 2001 From: Charisee Chiw Date: Fri, 4 Feb 2022 11:40:18 -0800 Subject: [PATCH 2/3] Update abort_unwind.md --- src/error/abort_unwind.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/error/abort_unwind.md b/src/error/abort_unwind.md index 4a845b4ee7..02b9ecf67c 100644 --- a/src/error/abort_unwind.md +++ b/src/error/abort_unwind.md @@ -10,11 +10,11 @@ Building on the prior lemonade example, we explicitly use the panic strategy to 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); } + 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() { @@ -35,8 +35,8 @@ fn ah(){ println!("Spit it out!!!!");} 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);} + if beverage == "lemonade"{ ah();} + else{println!("Some refreshing {} is all I need.", beverage);} } fn main() { From 0b3ccde0ca838787386fdbd66681cb34999ae5a0 Mon Sep 17 00:00:00 2001 From: Charisee Chiw Date: Fri, 4 Feb 2022 11:44:15 -0800 Subject: [PATCH 3/3] Update abort_unwind.md --- src/error/abort_unwind.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/error/abort_unwind.md b/src/error/abort_unwind.md index 02b9ecf67c..346a619ecd 100644 --- a/src/error/abort_unwind.md +++ b/src/error/abort_unwind.md @@ -6,7 +6,6 @@ The previous section illustrates the error handling mechanism `panic`. The `cfg Building on the prior lemonade example, we explicitly use the panic strategy to execise different lines of code. ```rust,editable,ignore,mdbook-runnable -#![feature(cfg_panic)] fn drink(beverage: &str) { // You shouldn't drink too much sugary beverages. @@ -26,7 +25,6 @@ fn main() { Here is another example focusing on rewriting `drink()` and explicitly use the `unwind` keyword. ```rust,editable,ignore -#![feature(cfg_panic)] #[cfg(panic = "unwind")] fn ah(){ println!("Spit it out!!!!");}