iter on vec
This commit is contained in:
parent
315bc1853f
commit
6eb958eb56
74
src/main.rs
74
src/main.rs
|
@ -1,73 +1,9 @@
|
|||
struct Library {
|
||||
books: Vec<Book>,
|
||||
}
|
||||
|
||||
struct Book {
|
||||
title: String,
|
||||
year: u16,
|
||||
}
|
||||
|
||||
impl Book {
|
||||
fn new(title: &str, year: u16) -> Book {
|
||||
Book {
|
||||
title: String::from(title),
|
||||
year,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Book {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{} ({})", self.title, self.year)
|
||||
}
|
||||
}
|
||||
|
||||
impl Library {
|
||||
fn new() -> Library {
|
||||
return Library { books: vec![] };
|
||||
}
|
||||
|
||||
fn len(&self) -> usize {
|
||||
return self.books.len();
|
||||
}
|
||||
|
||||
fn is_empty(&self) -> bool {
|
||||
return self.books.len() == 0;
|
||||
}
|
||||
|
||||
fn add_book(&mut self, book: Book) {
|
||||
self.books.push(book);
|
||||
}
|
||||
|
||||
fn print_books(&self) {
|
||||
if self.is_empty() {
|
||||
println!("no books");
|
||||
return;
|
||||
}
|
||||
for book in self.books.iter() {
|
||||
println!("\"{}\" ({})", book.title, book.year);
|
||||
}
|
||||
}
|
||||
|
||||
fn oldest_book(&self) -> Option<&Book> {
|
||||
self.books.iter().min_by(|x, y| x.year.cmp(&y.year))
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let library: &mut Library = &mut Library::new();
|
||||
let v: Vec<i8> = vec![10, 20, 30];
|
||||
let mut iter = v.iter();
|
||||
|
||||
println!("our Library is empty: {}", library.is_empty());
|
||||
println!("v[0]: {:?}", iter.next());
|
||||
|
||||
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());
|
||||
let v1: Option<&i8> = iter.next();
|
||||
println!("v[1]: {v1:?}");
|
||||
}
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue