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 @@ -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)
42 changes: 42 additions & 0 deletions src/challenges/challenge-66.md
Original file line number Diff line number Diff line change
@@ -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).
Loading