Skip to content

Commit 2832a65

Browse files
author
Charisee
committed
documenation for abort and unwind
1 parent 18c0055 commit 2832a65

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155

156156
- [Error handling](error.md)
157157
- [`panic`](error/panic.md)
158+
- [`abort` & `unwind`](error/abort_unwind.md)
158159
- [`Option` & `unwrap`](error/option_unwrap.md)
159160
- [Unpacking options with `?`](error/option_unwrap/question_mark.md)
160161
- [Combinators: `map`](error/option_unwrap/map.md)

src/error/abort_unwind.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# `abort` and `unwind`
2+
3+
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`.
4+
5+
6+
Building on the prior lemonade example, we explicitly use the panic strategy to execise different lines of code.
7+
8+
```rust,editable,ignore,mdbook-runnable
9+
#![feature(cfg_panic)]
10+
11+
fn drink(beverage: &str) {
12+
// You shouldn't drink too much sugary beverages.
13+
if beverage == "lemonade" {
14+
if cfg!(panic="abort"){ println!("This is not your party. Run!!!!");}
15+
else{ println!("Spit it out!!!!");}
16+
}
17+
else{ println!("Some refreshing {} is all I need.", beverage); }
18+
}
19+
20+
fn main() {
21+
drink("water");
22+
drink("lemonade");
23+
}
24+
```
25+
26+
Here is another example focusing on rewriting `drink()` and explicitly use the `unwind` keyword.
27+
28+
```rust,editable,ignore
29+
#![feature(cfg_panic)]
30+
31+
#[cfg(panic = "unwind")]
32+
fn ah(){ println!("Spit it out!!!!");}
33+
34+
#[cfg(not(panic="unwind"))]
35+
fn ah(){ println!("This is not your party. Run!!!!");}
36+
37+
fn drink(beverage: &str){
38+
if beverage == "lemonade"{ ah();}
39+
else{println!("Some refreshing {} is all I need.", beverage);}
40+
}
41+
42+
fn main() {
43+
drink("water");
44+
drink("lemonade");
45+
}
46+
```
47+
48+
The panic strategy can be set from the command line by using `abort` or `unwind`.
49+
50+
```rust,editable,ignore
51+
rustc lemonade.rc -C panic=abort
52+
```
53+

0 commit comments

Comments
 (0)