@@ -33,8 +33,9 @@ impl Book {
3333 }
3434}
3535
36- // Implement the methods below. Update the `self` parameter to
37- // indicate the method's required level of ownership over the object:
36+ // Implement the methods below. Notice how the `self` parameter
37+ // changes type to indicate the method's required level of ownership
38+ // over the object:
3839//
3940// - `&self` for shared read-only access,
4041// - `&mut self` for unique and mutable access,
@@ -49,49 +50,34 @@ impl Library {
4950 }
5051
5152 // ANCHOR: Library_len
52- //fn len(self) -> usize {
53- // todo!("Return the length of `self.books`")
54- //}
55- // ANCHOR_END: Library_len
5653 fn len ( & self ) -> usize {
54+ // ANCHOR_END: Library_len
5755 self . books . len ( )
5856 }
5957
6058 // ANCHOR: Library_is_empty
61- //fn is_empty(self) -> bool {
62- // todo!("Return `true` if `self.books` is empty")
63- //}
64- // ANCHOR_END: Library_is_empty
6559 fn is_empty ( & self ) -> bool {
60+ // ANCHOR_END: Library_is_empty
6661 self . books . is_empty ( )
6762 }
6863
6964 // ANCHOR: Library_add_book
70- //fn add_book(self, book: Book) {
71- // todo!("Add a new book to `self.books`")
72- //}
73- // ANCHOR_END: Library_add_book
7465 fn add_book ( & mut self , book : Book ) {
66+ // ANCHOR_END: Library_add_book
7567 self . books . push ( book)
7668 }
7769
7870 // ANCHOR: Library_print_books
79- //fn print_books(self) {
80- // todo!("Iterate over `self.books` and print each book's title and year")
81- //}
82- // ANCHOR_END: Library_print_books
8371 fn print_books ( & self ) {
72+ // ANCHOR_END: Library_print_books
8473 for book in & self . books {
8574 println ! ( "{}, published in {}" , book. title, book. year) ;
8675 }
8776 }
8877
8978 // ANCHOR: Library_oldest_book
90- //fn oldest_book(self) -> Option<&Book> {
91- // todo!("Return a reference to the oldest book (if any)")
92- //}
93- // ANCHOR_END: Library_oldest_book
9479 fn oldest_book ( & self ) -> Option < & Book > {
80+ // ANCHOR_END: Library_oldest_book
9581 // Using a closure and a built-in method:
9682 // self.books.iter().min_by_key(|book| book.year)
9783
@@ -108,30 +94,31 @@ impl Library {
10894}
10995
11096// ANCHOR: main
111- // This shows the desired behavior. Uncomment the code below and
112- // implement the missing methods. You will need to update the
113- // method signatures, including the "self" parameter! You may
114- // also need to update the variable bindings within main.
11597fn main ( ) {
116- let library = Library :: new ( ) ;
117-
118- //println!("The library is empty: library.is_empty() -> {}", library.is_empty());
119- //
120- //library.add_book(Book::new("Lord of the Rings", 1954));
121- //library.add_book(Book::new("Alice's Adventures in Wonderland", 1865));
122- //
123- //println!("The library is no longer empty: library.is_empty() -> {}", library.is_empty());
124- //
125- //
126- //library.print_books();
127- //
128- //match library.oldest_book() {
129- // Some(book) => println!("The oldest book is {}", book.title),
130- // None => println!("The library is empty!"),
131- //}
132- //
133- //println!("The library has {} books", library.len());
134- //library.print_books();
98+ let mut library = Library :: new ( ) ;
99+
100+ println ! (
101+ "The library is empty: library.is_empty() -> {}" ,
102+ library. is_empty( )
103+ ) ;
104+
105+ library. add_book ( Book :: new ( "Lord of the Rings" , 1954 ) ) ;
106+ library. add_book ( Book :: new ( "Alice's Adventures in Wonderland" , 1865 ) ) ;
107+
108+ println ! (
109+ "The library is no longer empty: library.is_empty() -> {}" ,
110+ library. is_empty( )
111+ ) ;
112+
113+ library. print_books ( ) ;
114+
115+ match library. oldest_book ( ) {
116+ Some ( book) => println ! ( "The oldest book is {}" , book. title) ,
117+ None => println ! ( "The library is empty!" ) ,
118+ }
119+
120+ println ! ( "The library has {} books" , library. len( ) ) ;
121+ library. print_books ( ) ;
135122}
136123// ANCHOR_END: main
137124
0 commit comments