Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,5 @@
- [Challenge 62](./challenges/challenge-62.md)
- [Challenge 63](./challenges/challenge-63.md)
- [Challenge 64](./challenges/challenge-64.md)
- [Challenge 65](./challenges/challenge-65.md)
- [Resources](./resources.md)
33 changes: 33 additions & 0 deletions src/challenges/challenge-65.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Challenge 65

### Rust Tip: Use std::panic::catch_unwind for Controlled Panic Handling

`std::panic::catch_unwind` is a lesser-known tool to catch and handle panics, preventing program crashes in sensitive contexts like FFI or testing.

It captures panics and returns a Result, allowing graceful recovery.

```rust
// Rust Bytes Issue 74: Use std::panic::catch_unwind for Controlled Panic Handling

use std::panic::catch_unwind;

fn risky_op -> i32 {
panic!("Oh no!");
}

fn main() {
let result = catch_unwind(|| risky_op());
match result {
Ok(val) => println!("Result: {}", val),
Err(_) => println!("Caught panic!"),
}
}
```


You can play around with the code on [Rust Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=ba2d516bc165ee52cc8c7bbe338f7321).

**Notes:** This function **might not catch all Rust panics.** A Rust panic is not always implemented via unwinding, but can be implemented by aborting the process as well. This function *only* catches unwinding panics, not those that abort the process.


Check the docs on [std::panic::catch_unwind](https://doc.rust-lang.org/std/panic/fn.catch_unwind.html) for more information.
Loading