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#[derive(Debug)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { BookItem::Book {pages, discount} => return Self::check_book_validity(pages, discount), BookItem::EBook(title, field) => return Self::check_ebook_validity(title.as_str(), field), BookItem::Collection(items) => return Self::check_collection_validity(&items), BookItem::OutOfPrint => false, } } fn check_book_validity(pages: &i32, discount: &Option<i32>) -> bool { *pages > 0 && match discount { Some(d) => *d >= 0 && *d <= 50, None => true, } } fn check_ebook_validity(title: &str, field: &(i32, i32)) -> bool { title.len() != 0 && field.1 > 0 } fn check_collection_validity(items: &Vec<BookItem>) -> bool { items.len() != 0 && { let mut is_collection_valid = true; for item in items { if !item.check_validity() { is_collection_valid = false; break } } is_collection_valid } }}
#[derive(Debug)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { BookItem::Book {pages, discount} => return Self::check_book_validity(pages, discount), BookItem::EBook(title, field) => return Self::check_ebook_validity(title.as_str(), field), BookItem::Collection(items) => return Self::check_collection_validity(&items), BookItem::OutOfPrint => false, } } fn check_book_validity(pages: &i32, discount: &Option<i32>) -> bool { *pages > 0 && match discount { Some(d) => *d >= 0 && *d <= 50, None => true, } } fn check_ebook_validity(title: &str, field: &(i32, i32)) -> bool { title.len() != 0 && field.1 > 0 } fn check_collection_validity(items: &Vec<BookItem>) -> bool { items.len() != 0 && { let mut is_collection_valid = true; for item in items { if !item.check_validity() { is_collection_valid = false; break } } is_collection_valid } }}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount } => { if *pages < 0 { return false; } match discount { Some(d) => (0..=50).contains(d), None => true, } } BookItem::EBook(title, (_, size)) => !title.is_empty() && *size > 0, BookItem::Collection(books) => { !books.is_empty() && books.iter().all(BookItem::check_validity) } BookItem::OutOfPrint => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount } => { if *pages < 0 { return false; } match discount { None => return true, Some(discount) => { *discount >0 && *discount < 50 } } } BookItem::EBook(title, (_, discount)) => { if title.is_empty() { return false; } if *discount < 1 { return false; } return true; } BookItem::Collection(book_items) => { if book_items.is_empty() { return false; } book_items.iter().all( |book_item| book_item.check_validity() ) } BookItem::OutOfPrint => { false } } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { Self::Book { pages, discount } if *pages > 0 => match discount { None => true, Some(value) if *value >= 0 && *value <= 50 => true, _ => false, }, Self::EBook(title, tuple) if !title.is_empty() && tuple.0 > 0 && tuple.1 > 0 => true, Self::Collection(items) if !items.is_empty() && items.iter().all(|item| item.check_validity()) => true, Self::OutOfPrint => false, _ => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages , discount } => { if *pages <= 0 {return false;} match discount { None => true, Some(discount) => { (1..=50).contains(discount) } } } BookItem::EBook(title, (_, discount)) => { !title.is_empty() && *discount > 0 } BookItem::Collection(book_list) => { if book_list.is_empty() {return false} book_list.iter().all(|book| book.check_validity()) } BookItem::OutOfPrint => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { Self::Book { pages: p, discount: d, } => { match *p { x if x < 0 => return false, _ => match *d { Some(x) => return x >= 0 && x <= 50, None => return true, }, }; } Self::EBook(s, (_, b)) => { return !s.is_empty() && *b > 0; } Self::Collection(vec) => match vec { x if x.is_empty() => return false, _ => vec.iter().all(|v| v.check_validity()), }, Self::OutOfPrint => return false, } // return true; }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { Self::Book { pages: p, discount: d, } => { match *p { x if x < 0 => return false, _ => match *d { Some(x) => return x >= 0 && x <= 50, None => return true, }, }; } Self::EBook(s, (_, b)) => { return !s.is_empty() && *b > 0; } Self::Collection(vec) => { match vec { x if x.is_empty() => return false, _ => { for v in vec.iter() { if !v.check_validity() { return false; } } return true; } } } Self::OutOfPrint => { return false; } } // return true; }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match &self { BookItem::Book { pages, discount } if *pages > 0 => match discount { None => true, Some(d) => *d > 0 && *d < 50 }, BookItem::EBook(s, p ) => !s.is_empty() && p.1 > 0, BookItem::Collection(v) => !v.is_empty() && v.iter().all(|i| i.check_validity()), _ => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount } => { *pages > 0 && (discount.is_none() || (0..=50).contains(&discount.unwrap())) } BookItem::EBook(title, tuple) => !title.is_empty() && tuple.1 > 0, BookItem::Collection(vec) => { !vec.is_empty() && vec.iter().all(|item| item.check_validity()) } BookItem::OutOfPrint => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool{ match self{ BookItem::Book{pages,discount: None} => *pages>0, BookItem::Book{pages,discount: Some(disc)} => *pages>0 && *disc>=0 && *disc <=50, BookItem::EBook(title,(_,x)) => !title.is_empty() && *x>0, BookItem::Collection(list) => !list.is_empty() && list.iter().all(|x| x.check_validity()), BookItem::OutOfPrint => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount: Some(d), } if *pages > 0 && *d >= 0 && *d <= 50 => true, BookItem::Book { pages, discount: None, } if *pages > 0 => true, BookItem::EBook(title, (_, y)) if !title.is_empty() && *y > 0 => true, BookItem::Collection(books) => books.len() > 0 && books.iter().all(|book| book.check_validity()), BookItem::OutOfPrint => false, _ => false, } }}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount } => { if *pages < 1 { return false; } if let Some(dsc) = discount { if *dsc < 0 { return false; } if *dsc > 50 { return false; } } return true; }, BookItem::EBook(title, (_,b)) => { if title == "" { return false; } if *b < 1 { return false; } return true; }, BookItem::Collection(items) => { if items.is_empty() { return false; } if !items.iter().all(|item| item.check_validity()) { return false; } return true; }, _ => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount } => { if *pages <= 0 { return false; } if let Some(discount) = discount { (0..=50).contains(discount) } else { true } } BookItem::EBook(title, (a, b)) => !title.is_empty() && 0 < *a && 0 < *b, BookItem::Collection(vec) => { if vec.is_empty() { return false; } vec.iter().all(|item| item.check_validity()) } BookItem::OutOfPrint => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount } => match discount { Some(x) => { if *x > 0 && *pages > 0 { if *x > 50 { false } else { true } } else { false } } None => { if *pages > 0 { true } else { false } } }, BookItem::EBook(x, y) => { if !x.is_empty() { if y.0 > 0 && y.1 > 0 { true } else { false } } else { false } } BookItem::Collection(items) => { if !items.is_empty() { items.iter().all(|item| item.check_validity()) } else { false } } _ => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount } => match discount { Some(x) => { if *x > 0 && *pages > 0 { if *x > 50 { false } else { true } } else { false } } None => { if *pages > 0 { true } else { false } } }, BookItem::EBook(x, y) => { if !x.is_empty() { if y.0 > 0 && y.1 > 0 { true } else { false } } else { false } } BookItem::Collection(items) => { if !items.is_empty() { items.iter().all(|item| item.check_validity()) } else { false } } _ => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Clone, Debug)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match &self { BookItem::Book{pages, discount} => { *pages > 0 && if let Some(discount) = *discount { 50 >= discount && 0 <= discount } else { true } }, BookItem::EBook(title, value) => { !title.is_empty() && value.1 > 0 }, BookItem::Collection(collection) => { !collection.is_empty() && !collection.iter().any(|b| !b.check_validity()) }, BookItem::OutOfPrint => false } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Clone, Debug)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount } => { *pages > 0 && if let Some(disc_val) = *discount { disc_val >= 0 && disc_val <= 50 } else { true } }, BookItem::EBook(title, (_, value)) => { !title.is_empty() && *value > 0 }, BookItem::Collection(list) => { list.len() > 0 && !list.iter().any(|x| {!x.check_validity()}) }, BookItem::OutOfPrint => { false }, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool { match self { Self::Book { pages, discount } => { *pages > 0 && (discount.is_none() || (discount.is_some_and(|n| n > 0) && discount.is_some_and(|n| n <= 50))) }, Self::EBook(title, (_, second)) => { !title.is_empty() && *second > 0 }, Self::Collection(items) => { !items.is_empty() && items.iter().all(|item| item.check_validity()) }, Self::OutOfPrint => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount } => { *pages > 0 && if let Some(d) = discount { (0..=50).contains(d) } else { true } } BookItem::EBook(title, (_, b)) => !title.is_empty() && *b > 0, BookItem::Collection(c) => !c.is_empty() && c.iter().all(|b| b.check_validity()), BookItem::OutOfPrint => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { pub fn check_validity(&self) -> bool { match self { BookItem::OutOfPrint => false, BookItem::Book{pages: pages, discount: Some(discount)} => {if *pages < 1 || *discount < 0 || *discount > 50 {false} else {true}}, BookItem::Book{pages: pages, discount: None} => *pages > 0, BookItem::EBook(title, (_, thing)) => {if title.is_empty() || *thing < 1 {false} else {true}}, BookItem::Collection(items) => items.len() > 0 && items.iter().all(|x| x.check_validity()), } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount } => { if *pages < 0 { return false } if let Some(d) = discount { if *d < 0 || *d > 50 { return false } } true }, BookItem::EBook(s, (_, b)) => { if s.is_empty() { return false } if *b <= 0 { return false } true }, BookItem::Collection(items) => { if items.is_empty() { return false } if items.iter().any(|b| !b.check_validity()) { return false } true }, BookItem::OutOfPrint => false, } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}
#[derive(Debug, Clone)]pub enum BookItem { Book { pages: i32, discount: Option<i32> }, EBook(String, (i32, i32)), Collection(Vec<BookItem>), OutOfPrint,}impl BookItem { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount } => { let page_valid = *pages > 0i32; if page_valid { if let Some(discount) = discount { *discount >= 0 && *discount <= 50 } else { true } } else { false } } BookItem::EBook(title, (a, b)) => { !title.is_empty() && *b > 0i32 } BookItem::Collection(items) => { !items.is_empty() && items.iter().all(|i| i.check_validity()) } BookItem::OutOfPrint => { false } } }}// Example usagepub fn main() { let book_a = BookItem::Book { pages: 42, discount: Some(100), }; let ebook_b = BookItem::EBook("hello".to_string(), (1, 2)); let collection_c = BookItem::Collection(vec![book_a.clone(), BookItem::OutOfPrint]); assert!( !book_a.check_validity(), "Book with discount > 50 should be invalid" ); assert!( ebook_b.check_validity(), "EBook with valid title and tuple should be valid" ); assert!( !collection_c.check_validity(), "Collection containing invalid items should be invalid" ); assert!( !BookItem::OutOfPrint.check_validity(), "OutOfPrint should always be invalid" );}