@@ -6,14 +6,17 @@ The previous section illustrates the error handling mechanism `panic`. Differen
6
6
Building on the prior lemonade example, we explicitly use the panic strategy to exercise different lines of code.
7
7
8
8
``` rust,editable,mdbook-runnable
9
-
10
9
fn drink(beverage: &str) {
11
- // You shouldn't drink too much sugary beverages.
10
+ // You shouldn't drink too much sugary beverages.
12
11
if beverage == "lemonade" {
13
- if cfg!(panic="abort"){ println!("This is not your party. Run!!!!");}
14
- else{ println!("Spit it out!!!!");}
12
+ if cfg!(panic = "abort") {
13
+ println!("This is not your party. Run!!!!");
14
+ } else {
15
+ println!("Spit it out!!!!");
16
+ }
17
+ } else {
18
+ println!("Some refreshing {} is all I need.", beverage);
15
19
}
16
- else{ println!("Some refreshing {} is all I need.", beverage); }
17
20
}
18
21
19
22
fn main() {
@@ -25,16 +28,22 @@ fn main() {
25
28
Here is another example focusing on rewriting ` drink() ` and explicitly use the ` unwind ` keyword.
26
29
27
30
``` rust,editable
28
-
29
31
#[cfg(panic = "unwind")]
30
- fn ah(){ println!("Spit it out!!!!");}
32
+ fn ah() {
33
+ println!("Spit it out!!!!");
34
+ }
31
35
32
- #[cfg(not(panic="unwind"))]
33
- fn ah(){ println!("This is not your party. Run!!!!");}
36
+ #[cfg(not(panic = "unwind"))]
37
+ fn ah() {
38
+ println!("This is not your party. Run!!!!");
39
+ }
34
40
35
- fn drink(beverage: &str){
36
- if beverage == "lemonade"{ ah();}
37
- else{println!("Some refreshing {} is all I need.", beverage);}
41
+ fn drink(beverage: &str) {
42
+ if beverage == "lemonade" {
43
+ ah();
44
+ } else {
45
+ println!("Some refreshing {} is all I need.", beverage);
46
+ }
38
47
}
39
48
40
49
fn main() {
0 commit comments