Skip to content

Commit

Permalink
Fix solution for Storing Books exercise (#1237)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mgeisler authored Sep 22, 2023
1 parent c4a821d commit 187fc20
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 45 deletions.
10 changes: 10 additions & 0 deletions src/exercises/day-2/book-library.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
77 changes: 32 additions & 45 deletions src/exercises/day-2/book-library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)

Expand All @@ -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();

//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();
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();
}
// ANCHOR_END: main

Expand Down

0 comments on commit 187fc20

Please sign in to comment.