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, 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} => { return *pages >= 0 && discount.map_or(true, |d| d >= 0 && d <= 50); }, BookItem::EBook(title, (l, r)) => !title.is_empty() && *r > 0, BookItem::Collection(items) => items.len() > 0 && items.iter().all(|i| i.check_validity()), BookItem::OutOfPrint => false, _ => panic!("unknown book item"), } }}// 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 } => { *pages > 0 && discount.map_or(true, |d| d <= 50 && d >= 0) } BookItem::EBook(title, tuple) => !title.is_empty() && tuple.0 > 0 && tuple.1 > 0, BookItem::Collection(items) => { items.len() > 0 && items.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 { Self::Book { pages: 1.., discount: Some(0..=50) | None } | Self::EBook(_, (_, 1..)) | Self::Collection(_) if self.non_pattern_validity_check() => true, Self::OutOfPrint | _ => false } } /// internal function used for if-guards fn non_pattern_validity_check(&self) -> bool { // fricken expressions not being pattern-able lol match self { Self::EBook(string, (_, _)) => !string.is_empty(), Self::Collection(vec) => { if vec.is_empty() { return false } vec.iter().all(Self::check_validity) }, Self::Book {..} | Self::OutOfPrint => 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: 1.., discount: None | Some(0..=50)} => true, Self::EBook(title, (_, 1..)) if !title.is_empty() => true, Self::Collection(items) if !items.is_empty() && items.iter().all(|book| book.check_validity()) => 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 < 1 { return false } match discount { None => return true, _ => { if discount.unwrap() > 50 || discount.unwrap() < 0 { return false } } } return true }, BookItem::EBook(n, (_x, y)) => { if n.len() == 0 { return false } if *y < 1 { return false } return true }, BookItem::Collection(n) => { if n.len() == 0 { return false } for item in n { if !item.check_validity() { return false } } return 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 { pub fn check_validity(&self) -> bool{ match self { BookItem::Book { pages, discount } => { if *pages <= 0 { return false; } match discount { Some(d) => { if *d < 0 || *d > 50 { return false; } return true; }, None => return true, } }, BookItem::EBook(title, tuple) => { if title.is_empty() { return false; } if tuple.1 <= 0 { return false; } return true; }, BookItem::Collection(book_items) => { if book_items.is_empty() { return false; } for book in book_items.iter() { if !book.check_validity() { return false; } } return true; }, BookItem::OutOfPrint => return false, } } // TODO: Finish the implementation}// 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: None } => *pages > 0, BookItem::Book { pages, discount: Some(discount) } => { *pages > 0 && *discount >= 0 && *discount <= 50 } BookItem::EBook(string, (_, second_field)) => !string.is_empty() && *second_field > 0, BookItem::Collection(book_items) => { !book_items.is_empty() && 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: 1.., discount: None | Some(0..=50) } => true, Self::EBook(title, (_, 1..)) if !title.is_empty() => true, Self::Collection(i) if !i.is_empty() && i.iter().all(|x| x.check_validity()) => 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 { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages: 1.., discount: None | Some(0..=50) } => true, // BookItem::Book { pages, discount } => { // if *pages <= 0 { // return false; // } // if let Some(discount) = discount { // return *discount >= 0 && *discount <= 50; // } // return true // }, BookItem::EBook(title, (_, b)) => title.len() > 0 && *b > 0, BookItem::Collection(books) => books.len() > 0 && books.iter().all(|book| book.check_validity()), BookItem::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 { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount } => { if *pages <= 0 { return false; } if let Some(discount) = discount { return *discount >= 0 && *discount <= 50; } return true }, BookItem::EBook(title, (_, b)) => title.len() > 0 && *b > 0, BookItem::Collection(books) => books.len() > 0 && books.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 { BookItem::Book { pages: 1.., discount: None | Some(0..=50) } => true, BookItem::EBook(title, (_, 1..)) if !title.is_empty() => true, BookItem::Collection(vec) if !vec.is_empty() && vec.iter().all(BookItem::check_validity) => 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 { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount } => { if let Some(d) = discount { return (*d >= 0) && (*d <= 50) && (*pages > 0); } else { return *pages > 0 } } BookItem::EBook(title, (_, i)) => { !title.is_empty() && (*i > 0) } BookItem::Collection(vec) => { !vec.is_empty() && vec.iter().map(|bi| bi.check_validity()).all(|x| x) } 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: 1.., discount: None | Some(0..=50) } => true, Self::EBook(title, (_, 1..)) if !title.is_empty() => true, Self::Collection(c) if !c.is_empty() && c.iter().all(BookItem::check_validity) => 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 { Self::Book { pages, discount: None } if *pages > 0 => true, Self::Book { pages, discount: Some(d) } if *pages > 0 && (0..=50).contains(d) => true, Self::EBook(title, (_, i)) if !title.is_empty() && *i > 0 => true, Self::Collection(c) if !c.is_empty() && c.iter().all(BookItem::check_validity) => 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 { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount } => *pages > 0 && discount.map_or(true, |d| d >= 0 && d <= 50), BookItem::EBook(title, (width, height)) => { !title.is_empty() && *width > 0 && *height > 0 } BookItem::Collection(items) => { !items.is_empty() && items.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 { Self::Book { pages, discount } => { if let Some(d) = discount { *pages > 0 && *d >= 0 && *d <= 50 } else { *pages > 0 } } Self::EBook(title, (width, height)) => { !title.is_empty() && *width > 0 && *height > 0 } Self::Collection(items) => { if items.is_empty() { false } else { 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 { Self::Book { pages, discount } => { if let Some(d) = discount { *pages > 0 && *d >= 0 && *d <= 50 } else { *pages > 0 } } Self::EBook(title, (width, height)) => { !title.is_empty() && *width > 0 && *height > 0 } Self::Collection(items) => { if items.is_empty() { false } else { 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 } if Self::__check_book(pages, discount) => true, BookItem::EBook(s, (_, i)) if !s.is_empty() && i.gt(&0) => true, BookItem::Collection(v) if !v.is_empty() && v.iter().all(|b| b.check_validity()) => true, _ => false, } } #[inline(always)] fn __check_book(pages: &i32, discount: &Option<i32>) -> bool { (*pages).gt(&0) && (discount.is_none() || (if let Some(d) = discount { d.ge(&0) && d.le(&50) } else { 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 } => { if *pages > 0{ match discount { Some(d) => { if *d > 0 && *d <= 50 { return true; }else{ return false; } }, None => { return true; } } } else{false} }, BookItem::EBook(title,(_,b) ) => { if title.len() > 0 && *b > 0{ return true; }else{ return false; } }, BookItem::Collection(items) => { if items.is_empty() { return false; } for item in items { if !item.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 { pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount } => { *pages > 0 && match discount { Some(value) => *value >= 0 && *value <= 50, None => true, } } BookItem::EBook(title, (_, b)) => !title.is_empty() && *b > 0, BookItem::Collection(books) => { !books.is_empty() && books.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 { pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount } => { *pages > 0 && match discount { Some(value) => *value >= 0 && *value <= 50, None => true, } } BookItem::EBook(title, (_, b)) => !title.is_empty() && *b > 0, BookItem::Collection(books) => { !books.is_empty() && books.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 { pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages, discount } => { match pages > &0 { true => match discount { Some(value) => { return &0 <= value && value <= &50; } None => return true, }, false => { return false; } }; } BookItem::EBook(title, tuple) => { return !title.is_empty() && tuple.1 > 0; } BookItem::Collection(items) => { return !items.is_empty() && items.iter().all(|item|item.check_validity()) } BookItem::OutOfPrint => { return 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} => { *pages > 0 && match discount { Some(d) => *d >= 0 && *d <= 50, None => true, } }, BookItem::EBook(title, (a, b)) => { !title.is_empty() && *a > 0 && *b > 0 }, BookItem::Collection(books) => { !books.is_empty() && books.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 { BookItem::Book {pages: p, discount: _ } if *p <= 0 => false, BookItem::Book {pages: _, discount: Some(d) } if *d < 0 || *d > 50 => false, BookItem::EBook(title, _) if title.is_empty() => false, BookItem::EBook(_, (_, v)) if *v <= 0 => false, BookItem::Collection(v) if v.is_empty() || !v.iter().all(|x| x.check_validity()) => false, BookItem::OutOfPrint => false, _ => 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: p, discount: _ } if *p <= 0 => false, BookItem::Book {pages: _, discount: Some(d) } if *d < 0 || *d > 50 => false, BookItem::EBook(title, _) if title.is_empty() => false, BookItem::EBook(_, (_, v)) if *v <= 0 => false, BookItem::Collection(v) if v.is_empty() || !v.iter().all(|x| x.check_validity()) => false, BookItem::OutOfPrint => false, _ => 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 { if let Some(d) = discount { if *d >= 0 as i32 && *d <= 50 { true } else { false } } else { true } } else { false } } BookItem::EBook(title, (_, second)) => { if !title.is_empty() && *second > 0 { true } else { false } } BookItem::Collection(vec) => { if !vec.is_empty() { vec.iter().all(|bi| bi.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 { // TODO: Finish the implementation pub fn check_validity(&self) -> bool { match self { BookItem::Book { pages: p, discount: _ } if *p <= 0 => false, BookItem::Book { pages: _, discount: None } => true, BookItem::Book { pages: _, discount: Some(d) } if (0..50).contains(d) => true, BookItem::EBook(title, (_, _)) if title.is_empty() => false, BookItem::EBook(_, (_, b)) if *b > 0 => true, BookItem::Collection(v) if v.is_empty() => false, BookItem::Collection(v) if v.iter().all(|i| i.check_validity()) => 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, PartialEq)]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(d) = discount { if *d < 0 || *d > 50 { return false; } } true }, BookItem::EBook(title, (num1, num2)) => { if title.trim().is_empty() { return false; } if *num2 <= 0 { return false; } true }, BookItem::Collection(items) => { if items.is_empty() { return false; } items.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 } if *pages > 0 && discount.map_or(true, |d| (0..=50).contains(&d)) => true, BookItem::EBook(title, (_, pages)) if !title.is_empty() && *pages > 0 => true, BookItem::Collection(items) if !items.is_empty() && items.iter().all(|item| item.check_validity()) => 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 } => { *pages > 0 && (discount.is_some() && discount.unwrap() > 0 && discount.unwrap() <= 50 || discount.is_none()) } BookItem::Collection(items) => { !items.is_empty() && items.iter().all(|b| b.check_validity() == true) } // BookItem::OutOfPrint => false, BookItem::EBook(title, other) => !title.is_empty() && other.1 > 0, _ => 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.map_or(true, |discount| (0..=50).contains(&discount)), BookItem::EBook(title, (_, b)) => !title.is_empty() && *b > 0, BookItem::Collection(books) => !books.is_empty() && books.iter().all(Self::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, PartialEq)]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.map_or(true, |d| d <= 50 && d >= 0), BookItem::EBook(title, price) => !title.is_empty() && price.0 > 0 && price.1 > 0, BookItem::Collection(items) => !items.is_empty() && items.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(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 { Self::Book{pages, discount} => Self::check_book(*pages, *discount), Self::EBook(tittle, (_,second_element)) => Self::check_Ebook(tittle, *second_element), Self::Collection(data_collection) => Self::check_collection(data_collection), _ => false, } } fn check_collection(collection : &Vec<BookItem> ) -> bool{ ! collection.is_empty() && collection.iter().all(|item| item.check_validity()) } fn check_Ebook(tittle: &str, second_element:i32)-> bool { !(tittle.is_empty() || second_element <= 0) } fn check_book(pages: i32, discount:Option<i32>) -> bool { let discount = discount.unwrap_or(0); pages > 0 && (discount >= 0 && discount <= 50) }}// 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" );}
use std::ops::Not;#[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(discount) = discount { return *discount <= 50 && *discount >= 0 } true }, BookItem::EBook(title, (first, second)) => title.is_empty().not() && *first > 0 && *second > 0, BookItem::Collection(books) => books.len() > 0 && books.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 { // 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" );}