diff --git a/src/main.rs b/src/main.rs index 2d3eaad..e6a890e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ mod float; mod signed; mod string; mod unsigned; +mod user_book_constructor; mod user_struct; fn main() { @@ -11,4 +12,5 @@ fn main() { float::intro_to_float(); string::strings(); user_struct::user_registry(); + user_book_constructor::book_new(); } diff --git a/src/user_book_constructor.rs b/src/user_book_constructor.rs new file mode 100644 index 0000000..996e971 --- /dev/null +++ b/src/user_book_constructor.rs @@ -0,0 +1,28 @@ +#[derive(Debug)] +#[warn(dead_code)] +pub struct Book<'a> { + // 1. Define the fields of the struct + // Make all of them public with `pub` + // Read the description for the fields + pub title: &'a str, + pub author: &'a str, + pub year: i64, + pub likes: u16, +} + +pub fn book_new() { + let book = Book::new("Get it right", "martin luther vibes", 2026); + println!("this is the result of the new book {:#?}", book) +} + +impl Book<'static> { + // 2. Define the `new` associated function + pub fn new<'a>(title: &'a str, author: &'a str, year: i64) -> Book<'a> { + Book { + title, + author, + year, + likes: 0, + } + } +}