mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-06-07 10:06:22 +02:00
Fix solution for Storing Books exercise (#1237)
As pointed out by @njr0 in #1233, we were making the exercise confusing by showing people code that cannot work — and then expecting the course participants to somehow fix this, without setting clear boundaries for what can and cannot be modified. This PR should align the exercise with the other exercises in the course and avoid the “brain teaser” here. This also has the advantage of having a full working solution, with no commented code which will bit-rot over time.
This commit is contained in:
parent
c4a821d43d
commit
187fc20007
@ -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}}
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user