Skip to content

Commit 77e5504

Browse files
added documentation for cfg_panic
1 parent 5e2d725 commit 77e5504

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/attributes/testing.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,50 @@ fn mytest() {
8181
assert_eq!(1, 2, "values don't match");
8282
}
8383
```
84+
### Different panic strategies
85+
86+
The `cfg_panic` feature makes it possible to exercise different lines of code depending on the panic strategy.
87+
The possible value is either `unwind` or `abort`.
88+
The following is a playful example on choosing the right beverage.
89+
90+
```toml
91+
#![feature(cfg_panic)]
92+
93+
#[cfg(panic = "unwind")]
94+
fn ah(){ println!("Spit it out!!!!");}
95+
96+
#[cfg(not(panic="unwind"))]
97+
fn ah(){ println!("This is not your party. Run!!!!");}
98+
99+
fn drink(beverage: &str){
100+
if beverage == "lemonade"{ ah();}
101+
else{println!("Some refreshing {} is all I need.", beverage);}
102+
}
103+
104+
fn main(){
105+
drink("water");
106+
drink("lemonade");
107+
}
108+
```
109+
110+
Here is the same example rewritten.
111+
```toml
112+
#![feature(cfg_panic)]
113+
114+
fn drink(beverage: &str) {
115+
// You shouldn't drink too much sugary beverages.
116+
if beverage == "lemonade" {
117+
if cfg!(panic="abort"){ println!("This is not your party. Run!!!!");}
118+
else{ println!("Spit it out!!!!");}
119+
}
120+
else{ println!("Some refreshing {} is all I need.", beverage); }
121+
}
122+
123+
fn main() {
124+
drink("water");
125+
drink("lemonade");
126+
}
127+
```
84128

85129
[_MetaListNameValueStr_]: ../attributes.md#meta-item-attribute-syntax
86130
[_MetaNameValueStr_]: ../attributes.md#meta-item-attribute-syntax

0 commit comments

Comments
 (0)