From 140f6446b1df390d998d747fe39eaa9aa16bcd8c Mon Sep 17 00:00:00 2001 From: Abeeujah Date: Wed, 13 Aug 2025 14:35:00 +0100 Subject: [PATCH 1/2] dev: Use std::panic::catch_unwind for Controlled Panic Handling (Issue 74) --- src/SUMMARY.md | 1 + src/challenges/challenge-65.md | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/challenges/challenge-65.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 918d42d..624c8a5 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -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) diff --git a/src/challenges/challenge-65.md b/src/challenges/challenge-65.md new file mode 100644 index 0000000..5687340 --- /dev/null +++ b/src/challenges/challenge-65.md @@ -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. From 1d10f474da037e1e5b4c3537f2653f712f7e2169 Mon Sep 17 00:00:00 2001 From: Abeeujah Date: Wed, 13 Aug 2025 14:51:47 +0100 Subject: [PATCH 2/2] dev: Rust Bytes Issue 75: Using matches! Macro for Concise Pattern Matching --- src/SUMMARY.md | 1 + src/challenges/challenge-66.md | 42 ++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/challenges/challenge-66.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 624c8a5..e25d7a9 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -67,4 +67,5 @@ - [Challenge 63](./challenges/challenge-63.md) - [Challenge 64](./challenges/challenge-64.md) - [Challenge 65](./challenges/challenge-65.md) + - [Challenge 66](./challenges/challenge-66.md) - [Resources](./resources.md) diff --git a/src/challenges/challenge-66.md b/src/challenges/challenge-66.md new file mode 100644 index 0000000..f3ccc68 --- /dev/null +++ b/src/challenges/challenge-66.md @@ -0,0 +1,42 @@ +# Challenge 66 + +### Rust Tip: Using matches! Macro for Concise Pattern Matching + +The `matches!` macro checks if a value matches a pattern, returning a boolean without requiring a full match expression. + +It's ideal for concise conditionals, especially with enums or complex types, avoiding verbose pattern matching. + +```rust +// Rust Bytes Issue 75: Using matches! Macro for Concise Pattern Matching + +#[allow(dead_code)] +enum Status { + Active, + Inactive, + Pending(u32), +} + +fn main() { + let statuses = vec![ + Status::Active, + Status::Inactive, + Status::Pending(42), + ]; + + for status in statuses { + if matches!(status, Status::Pending(_)) { + println!("Found a pending status!"); + } else if matches!(status, Status::Active) { + println!("Found an active status!"); + } + } + + // Example with more complex pattern + let value = Some(10); + if matches!(value, Some(x) if x > 5) { + println!("Value is Some and greater than 5!"); + } +} +``` + +You can play around with the code on [Rust Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=736aeb158cbf8013841d6c85ce6a3f58).