fnmain() { lettext = String::from("The quick brown fox jumps over the lazy dog."); letfox = Highlight(&text[4..19]); letdog = Highlight(&text[35..43]); // erase(text); // 报错:move out of `text` occurs here println!("{fox:?}"); println!("{dog:?}"); }
// TODO: remove this when you're done with your implementation. #![allow(unused_variables, dead_code)]
structLibrary { books: Vec<Book>, }
structBook { title: String, year: u16, }
implBook { // This is a constructor, used below. fnnew(title: &str, year: u16) -> Book { Book { title: String::from(title), year, } } }
// This makes it possible to print Book values with {}. implstd::fmt::Display forBook { fnfmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{} ({})", self.title, self.year) } }
fnmain() { // 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! letlibrary = Library::new();
//println!("Our 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)); // //library.print_books(); // //match library.oldest_book() { // Some(book) => println!("My oldest book is {book}"), // None => println!("My library is empty!"), //} // //println!("Our library has {} books", library.len()); }
implBook { // This is a constructor, used below. fnnew(title: &str, year: u16) -> Book { Book { title: String::from(title), year, } } }
// This makes it possible to print Book values with {}. implstd::fmt::Display forBook { fnfmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{} ({})", self.title, self.year) } }
// 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. fnmain() { letmut library = Library::new();
println!("Our 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));
library.print_books();
match library.oldest_book() { Some(book) => println!("My oldest book is {book}"), None => println!("My library is empty!"), }
println!("Our library has {} books", library.len()); }