day 1
This commit is contained in:
parent
6c6eb77480
commit
b6b32e2a3d
8
class/library/Cargo.toml
Normal file
8
class/library/Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "library"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
77
class/library/src/main.rs
Normal file
77
class/library/src/main.rs
Normal file
@ -0,0 +1,77 @@
|
||||
struct Library {
|
||||
books: Vec<Book>,
|
||||
}
|
||||
|
||||
struct Book {
|
||||
title: String,
|
||||
year: u16,
|
||||
}
|
||||
|
||||
impl Book {
|
||||
// This is a constructor, used below.
|
||||
fn new(title: &str, year: u16) -> Book {
|
||||
Book {
|
||||
title: String::from(title),
|
||||
year,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Implement the methods below. Update the `self` parameter 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,
|
||||
// - `self` for unique access by value.
|
||||
impl Library {
|
||||
fn new() -> Library {
|
||||
Library::new()
|
||||
Library { books: Vec::new()}
|
||||
}
|
||||
|
||||
fn len(self) -> usize {
|
||||
self.len()
|
||||
}
|
||||
|
||||
//fn is_empty(self) -> bool {
|
||||
// todo!("Return `true` if `self.books` is empty")
|
||||
//}
|
||||
|
||||
//fn add_book(self, book: Book) {
|
||||
// todo!("Add a new book to `self.books`")
|
||||
//}
|
||||
|
||||
//fn print_books(self) {
|
||||
// todo!("Iterate over `self.books` and each book's title and year")
|
||||
//}
|
||||
|
||||
//fn oldest_book(self) -> Option<&Book> {
|
||||
// todo!("Return a reference to the oldest book (if any)")
|
||||
//}
|
||||
}
|
||||
|
||||
// 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.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.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();
|
||||
}
|
17
class/notes
17
class/notes
@ -158,5 +158,20 @@ traits!
|
||||
type conversions in a generic way
|
||||
x.into() will do the type conversion for you, if valid and lossless
|
||||
|
||||
const - compile time const, like #DEFINE
|
||||
|
||||
value of shadowing - one of the benefits of dynamic typed -- you have the same logical object, but it's transforming, you wanna keep the same name
|
||||
|
||||
memory management
|
||||
|
||||
stack / heap
|
||||
|
||||
shared xor mutable
|
||||
|
||||
explicit lifetimes:
|
||||
tick a
|
||||
var<'a>(p1: &'a Point, p2: &'a Point') -> &'a Point{}
|
||||
|
||||
erase(var) // you can delete a var...
|
||||
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user