Pattern matching in Rust is a powerful feature that allows you to destructure and inspect complex data types in a concise and readable manner. By using match statements, you can handle different variants of enums, tuples, and other data structures, applying specific logic based on the shape and content of the data.
This challenge will help you practice advanced pattern matching techniques in a real-world scenario.
You need to implement a method check_validity(&self)
for the BookItem
enum that returns a bool
indicating whether the item is valid based on the following rules:
Book:
EBook:
String
) must be non-empty.Collection:
OutOfPrint:
check_validity(&self)
using match guards to reflect these rules.true
or false
) based on the type of BookItem
.if
guards to combine multiple conditions: if *pages > 0 && *d >= 0
Option<i32>
using Some(d)
and None
!items.is_empty()
firstiter().all(|item| item.check_validity())
to ensure all items are valid*pages
, *second
_ => false
arm handles any remaining invalid cases