From b6b32e2a3de3ef79887ecb6288ba32f61181283a Mon Sep 17 00:00:00 2001 From: Luke Tidd Date: Thu, 11 May 2023 13:05:37 -0400 Subject: [PATCH] day 1 --- class/library/Cargo.toml | 8 ++++ class/library/src/main.rs | 77 +++++++++++++++++++++++++++++++++++++++ class/notes | 17 ++++++++- 3 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 class/library/Cargo.toml create mode 100644 class/library/src/main.rs diff --git a/class/library/Cargo.toml b/class/library/Cargo.toml new file mode 100644 index 0000000..0d4367c --- /dev/null +++ b/class/library/Cargo.toml @@ -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] diff --git a/class/library/src/main.rs b/class/library/src/main.rs new file mode 100644 index 0000000..70348fb --- /dev/null +++ b/class/library/src/main.rs @@ -0,0 +1,77 @@ +struct Library { + books: Vec, +} + +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(); +} diff --git a/class/notes b/class/notes index e0cbffe..a111208 100644 --- a/class/notes +++ b/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... + -