File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change @@ -81,6 +81,50 @@ fn mytest() {
81
81
assert_eq! (1 , 2 , " values don't match" );
82
82
}
83
83
```
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
+ ```
84
128
85
129
[ _MetaListNameValueStr_ ] : ../attributes.md#meta-item-attribute-syntax
86
130
[ _MetaNameValueStr_ ] : ../attributes.md#meta-item-attribute-syntax
You can’t perform that action at this time.
0 commit comments