Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation PR for cfg_panic #1157

Merged
merged 10 commits into from
Mar 15, 2022
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/attributes/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,48 @@ fn mytest() {
assert_eq!(1, 2, "values don't match");
}
```
## Different panic strategies

The `cfg_panic` feature makes it possible to exercise different lines of code depending on the panic strategy.
The possible value is either `unwind` or `abort`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this say make clear that code compiled with #[cfg(panic = "unwind")] being true may still aborting on panic if any crate is compiled with -Cpanic=abort?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good thing to point out. Note, I'm not as familiar with crates so i don't know too much about this but I'll take your word for it.

I updated the file, let me know what you think :)

The following is a playful example on choosing the right beverage.

```rust

#[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");
}
```

Here is the same example rewritten.
```rust

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");
}
```

[_MetaListNameValueStr_]: ../attributes.md#meta-item-attribute-syntax
[_MetaNameValueStr_]: ../attributes.md#meta-item-attribute-syntax
Expand Down