1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-06-18 15:17:34 +02:00

Provide more instructions in the library exercise (#589)

This commit is contained in:
Martin Geisler
2023-04-29 05:11:12 +02:00
committed by GitHub
parent fc68829e14
commit 244e5b99c6
2 changed files with 41 additions and 32 deletions

View File

@ -7,8 +7,9 @@ you just need to know part of its API:
fn main() { fn main() {
let mut vec = vec![10, 20]; let mut vec = vec![10, 20];
vec.push(30); vec.push(30);
println!("middle value: {}", vec[vec.len() / 2]); let midpoint = vec.len() / 2;
for item in vec.iter() { println!("middle value: {}", vec[midpoint]);
for item in &vec {
println!("item: {item}"); println!("item: {item}");
} }
} }
@ -18,11 +19,9 @@ Use this to create a library application. Copy the code below to
<https://play.rust-lang.org/> and update the types to make it compile: <https://play.rust-lang.org/> and update the types to make it compile:
```rust,should_panic ```rust,should_panic
{{#include book-library.rs:setup}} {{#include book-library.rs:setup}}
{{#include book-library.rs:Library_new}} {{#include book-library.rs:Library_new}}
unimplemented!() todo!("Initialize and return a `Library` value")
} }
{{#include book-library.rs:Library_len}} {{#include book-library.rs:Library_len}}

View File

@ -32,16 +32,16 @@ impl Book {
} }
} }
// This makes it possible to print Book values with {}. // Implement the methods below. Update the `self` parameter to
impl std::fmt::Display for Book { // indicate the method's required level of ownership over the object:
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { //
write!(f, "{} ({})", self.title, self.year) // - `&self` for shared read-only access,
} // - `&mut self` for unique and mutable access,
} // - `self` for unique access by value.
impl Library {
// ANCHOR_END: setup // ANCHOR_END: setup
// ANCHOR: Library_new // ANCHOR: Library_new
impl Library {
fn new() -> Library { fn new() -> Library {
// ANCHOR_END: Library_new // ANCHOR_END: Library_new
Library { books: Vec::new() } Library { books: Vec::new() }
@ -49,7 +49,7 @@ impl Library {
// ANCHOR: Library_len // ANCHOR: Library_len
//fn len(self) -> usize { //fn len(self) -> usize {
// unimplemented!() // todo!("Return the length of `self.books`")
//} //}
// ANCHOR_END: Library_len // ANCHOR_END: Library_len
fn len(&self) -> usize { fn len(&self) -> usize {
@ -58,7 +58,7 @@ impl Library {
// ANCHOR: Library_is_empty // ANCHOR: Library_is_empty
//fn is_empty(self) -> bool { //fn is_empty(self) -> bool {
// unimplemented!() // todo!("Return `true` if `self.books` is empty")
//} //}
// ANCHOR_END: Library_is_empty // ANCHOR_END: Library_is_empty
fn is_empty(&self) -> bool { fn is_empty(&self) -> bool {
@ -67,7 +67,7 @@ impl Library {
// ANCHOR: Library_add_book // ANCHOR: Library_add_book
//fn add_book(self, book: Book) { //fn add_book(self, book: Book) {
// unimplemented!() // todo!("Add a new book to `self.books`")
//} //}
// ANCHOR_END: Library_add_book // ANCHOR_END: Library_add_book
fn add_book(&mut self, book: Book) { fn add_book(&mut self, book: Book) {
@ -76,22 +76,33 @@ impl Library {
// ANCHOR: Library_print_books // ANCHOR: Library_print_books
//fn print_books(self) { //fn print_books(self) {
// unimplemented!() // todo!("Iterate over `self.books` and each book's title and year")
//} //}
// ANCHOR_END: Library_print_books // ANCHOR_END: Library_print_books
fn print_books(&self) { fn print_books(&self) {
for book in &self.books { for book in &self.books {
println!("{}", book); println!("{}, published in {}", book.title, book.year);
} }
} }
// ANCHOR: Library_oldest_book // ANCHOR: Library_oldest_book
//fn oldest_book(self) -> Option<&Book> { //fn oldest_book(self) -> Option<&Book> {
// unimplemented!() // todo!("Return a reference to the oldest book (if any)")
//} //}
// ANCHOR_END: Library_oldest_book // ANCHOR_END: Library_oldest_book
fn oldest_book(&self) -> Option<&Book> { fn oldest_book(&self) -> Option<&Book> {
self.books.iter().min_by_key(|book| book.year) // Using a closure and a built-in method:
// self.books.iter().min_by_key(|book| book.year)
// Longer hand-written solution:
let mut oldest: Option<&Book> = None;
for book in self.books.iter() {
if oldest.is_none() || book.year < oldest.unwrap().year {
oldest = Some(book);
}
}
oldest
} }
} }
@ -103,24 +114,23 @@ impl Library {
fn main() { fn main() {
let library = Library::new(); let library = Library::new();
//println!("Our library is empty: {}", library.is_empty()); //println!("The library is empty: {}", library.is_empty());
//
let favorite_book = Book::new("Lord of the Rings", 1954); //library.add_book(Book::new("Lord of the Rings", 1954));
println!("Our favorite book {favorite_book} should go in the library");
//library.add_book(favorite_book);
//library.add_book(Book::new("Alice's Adventures in Wonderland", 1865)); //library.add_book(Book::new("Alice's Adventures in Wonderland", 1865));
// //
//println!("The library is no longer empty: {}", library.is_empty());
//
//
//library.print_books(); //library.print_books();
// //
//match library.oldest_book() { //match library.oldest_book() {
// Some(book) => println!("My oldest book is {book}"), // Some(book) => println!("The oldest book is {book}"),
// None => println!("My library is empty!"), // None => println!("The library is empty!"),
//} //}
// //
//println!("Our library has {} books", library.len()); //println!("The library has {} books", library.len());
for book in library.books { //library.print_books();
println!("{book}");
}
} }
// ANCHOR_END: main // ANCHOR_END: main