diff --git a/src/exercises/day-2/book-library.md b/src/exercises/day-2/book-library.md index 23954e9a..b113b0e2 100644 --- a/src/exercises/day-2/book-library.md +++ b/src/exercises/day-2/book-library.md @@ -25,14 +25,24 @@ Use this to model a library's book collection. Copy the code below to } {{#include book-library.rs:Library_len}} + todo!("Return the length of `self.books`") + } {{#include book-library.rs:Library_is_empty}} + todo!("Return `true` if `self.books` is empty") + } {{#include book-library.rs:Library_add_book}} + todo!("Add a new book to `self.books`") + } {{#include book-library.rs:Library_print_books}} + todo!("Iterate over `self.books` and print each book's title and year") + } {{#include book-library.rs:Library_oldest_book}} + todo!("Return a reference to the oldest book (if any)") + } } {{#include book-library.rs:main}} diff --git a/src/exercises/day-2/book-library.rs b/src/exercises/day-2/book-library.rs index 14dd6d17..16cf04b5 100644 --- a/src/exercises/day-2/book-library.rs +++ b/src/exercises/day-2/book-library.rs @@ -33,8 +33,9 @@ impl Book { } } -// Implement the methods below. Update the `self` parameter to -// indicate the method's required level of ownership over the object: +// Implement the methods below. Notice how the `self` parameter +// changes type to indicate the method's required level of ownership +// over the object: // // - `&self` for shared read-only access, // - `&mut self` for unique and mutable access, @@ -49,49 +50,34 @@ impl Library { } // ANCHOR: Library_len - //fn len(self) -> usize { - // todo!("Return the length of `self.books`") - //} - // ANCHOR_END: Library_len fn len(&self) -> usize { + // ANCHOR_END: Library_len self.books.len() } // ANCHOR: Library_is_empty - //fn is_empty(self) -> bool { - // todo!("Return `true` if `self.books` is empty") - //} - // ANCHOR_END: Library_is_empty fn is_empty(&self) -> bool { + // ANCHOR_END: Library_is_empty self.books.is_empty() } // ANCHOR: Library_add_book - //fn add_book(self, book: Book) { - // todo!("Add a new book to `self.books`") - //} - // ANCHOR_END: Library_add_book fn add_book(&mut self, book: Book) { + // ANCHOR_END: Library_add_book self.books.push(book) } // ANCHOR: Library_print_books - //fn print_books(self) { - // todo!("Iterate over `self.books` and print each book's title and year") - //} - // ANCHOR_END: Library_print_books fn print_books(&self) { + // ANCHOR_END: Library_print_books for book in &self.books { println!("{}, published in {}", book.title, book.year); } } // ANCHOR: Library_oldest_book - //fn oldest_book(self) -> Option<&Book> { - // todo!("Return a reference to the oldest book (if any)") - //} - // ANCHOR_END: Library_oldest_book fn oldest_book(&self) -> Option<&Book> { + // ANCHOR_END: Library_oldest_book // Using a closure and a built-in method: // self.books.iter().min_by_key(|book| book.year) @@ -108,30 +94,31 @@ impl Library { } // ANCHOR: main -// This shows the desired behavior. Uncomment the code below and -// implement the missing methods. You will need to update the -// method signatures, including the "self" parameter! You may -// also need to update the variable bindings within main. fn main() { - let library = Library::new(); + let mut library = Library::new(); - //println!("The library is empty: library.is_empty() -> {}", library.is_empty()); - // - //library.add_book(Book::new("Lord of the Rings", 1954)); - //library.add_book(Book::new("Alice's Adventures in Wonderland", 1865)); - // - //println!("The library is no longer empty: library.is_empty() -> {}", library.is_empty()); - // - // - //library.print_books(); - // - //match library.oldest_book() { - // Some(book) => println!("The oldest book is {}", book.title), - // None => println!("The library is empty!"), - //} - // - //println!("The library has {} books", library.len()); - //library.print_books(); + println!( + "The library is empty: library.is_empty() -> {}", + library.is_empty() + ); + + library.add_book(Book::new("Lord of the Rings", 1954)); + library.add_book(Book::new("Alice's Adventures in Wonderland", 1865)); + + println!( + "The library is no longer empty: library.is_empty() -> {}", + library.is_empty() + ); + + library.print_books(); + + match library.oldest_book() { + Some(book) => println!("The oldest book is {}", book.title), + None => println!("The library is empty!"), + } + + println!("The library has {} books", library.len()); + library.print_books(); } // ANCHOR_END: main