Skip to content

Commit 187fc20

Browse files
authored
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.
1 parent c4a821d commit 187fc20

File tree

2 files changed

+42
-45
lines changed

2 files changed

+42
-45
lines changed

src/exercises/day-2/book-library.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,24 @@ Use this to model a library's book collection. Copy the code below to
2525
}
2626
2727
{{#include book-library.rs:Library_len}}
28+
todo!("Return the length of `self.books`")
29+
}
2830
2931
{{#include book-library.rs:Library_is_empty}}
32+
todo!("Return `true` if `self.books` is empty")
33+
}
3034
3135
{{#include book-library.rs:Library_add_book}}
36+
todo!("Add a new book to `self.books`")
37+
}
3238
3339
{{#include book-library.rs:Library_print_books}}
40+
todo!("Iterate over `self.books` and print each book's title and year")
41+
}
3442
3543
{{#include book-library.rs:Library_oldest_book}}
44+
todo!("Return a reference to the oldest book (if any)")
45+
}
3646
}
3747
3848
{{#include book-library.rs:main}}

src/exercises/day-2/book-library.rs

Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
11597
fn 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

Comments
 (0)