Unlike other programming languages, Rust doesn't have a build-in constructor
keyword to construct instances of a struct. Instead, you can directly create an instance of a struct simply by calling the struct's name and passing the required values.
However, this pattern is discouraged, in real-life scenarios, we might not want to expose all fields with pub
or some fields will have default values that it would be repetitive to provide the value for every new instance, instead.
In Rust we have a convention, constructors are implemented as associated functions named new
. They encapsulate the initialization of the struct, ensuring that instances are always created in a valid state.
For example, for the Post
struct above, we can have a default value for the views
to always be 0
for new instances.
In this challenge, you will implement a constructor for a struct that represents a Book. A Book
will have the following fields:
title
: A string that represents the book's title.author
: A string that represents the book's author.year
: An integer that represents the year the book was published.likes
: An integer that represents the number of likes the book has received. Default value is 0
.The constructor function, Book::new
, should take three parameters (title
, author
, and year
) and return a fully initialized Book
.
Implement a constructor function Book::new
that:
title: &str
, author: &str
, and year
(integer).Book
instance with the specified values and default likes
value of 0
.Remember to use pub
for fields (required for testing).
pub struct Book { // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields pub title: String, pub author: String, pub year: i32, pub likes: i32,}impl Book { // 2. Define the `new` associated function pub fn new(title: &str, author: &str, year: i32) -> Book { Book { title: title.to_string(), author: author.to_string(), year, likes: 0, } }}
pub struct Book { // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields pub title: String, pub author: String, pub year: isize, pub likes: usize,}impl Book { // 2. Define the `new` associated function pub fn new(title: &str, author: &str, year: isize) -> Book { Book { title: title.to_string(), author: author.to_string(), year, likes: 0, } }}
pub struct Book { pub title: String, pub author: String, pub year: i16, pub likes: i32}impl Book { pub fn new(title: &str, author: &str, year: i16) -> Self { Book { title: title.to_string(), author: author.to_string(), year, likes: 0 } }}
pub struct Book { // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields pub title: String, pub author: String, pub year: i32, pub likes: i32,}impl Book { // 2. Define the `new` associated function pub fn new(title: &str, author: &str, year: i32) -> Self { Book { title: title.to_string(), author: author.to_string(), year, likes: 0, } }}
pub struct Book { // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields pub title: String, pub author: String, pub year: i32, pub likes: i32,}impl Book { // 2. Define the `new` associated function pub fn new(title: &str, author: &str, year: i32) -> Self{ Self { title: title.to_string(), author: author.to_string(), year, likes: 0, } }}
pub struct Book { // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields pub title: String, pub author: String, pub year: i32, pub likes: i32,}impl Book { // 2. Define the `new` associated function pub fn new(title: &str, author: &str, year: i32) -> Self { Book { title: String::from(title), author: String::from(author), year, likes: 0, } }}
pub struct Book { pub title: String, pub author: String, pub year: i32, pub likes: i32}impl Book { pub fn new(title: &str, author: &str, year: i32) -> Book { return Book { title: String::from(title), author: String::from(author), year, likes: 0 }; }}
pub struct Book { // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields pub title: String, pub author: String, pub year: i32, pub likes: u32,}impl Book { // 2. Define the `new` associated function pub fn new(title: &str, author: &str, year: i32) -> Self { Book { title: title.to_string(), author: author.to_string(), year: year, likes: 0, } }}
pub struct Book { // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields pub title: String, pub author: String, pub year: i32, pub likes: u64,}impl Book { // 2. Define the `new` associated function pub fn new(title: &str, author: &str, year: i32) -> Self { Self { title: title.to_owned(), author: author.to_owned(), year, likes: 0, } }}
pub struct Book { pub title: String, pub author: String, pub year: i32, pub likes: u32,}impl Book { pub fn new(title: &str, author: &str, year: i32) -> Self { Book { title: title.to_string(), author: author.to_string(), year, likes: 0, } }}
pub struct Book { pub title: String, pub author: String, pub year: i32, pub likes: i32}impl Book { pub fn new(title: &str, author: &str, year: i32) -> Book { return Book { title: String::from(title), author: String::from(author), year, likes: 0 }; }}
pub struct Book { // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields pub year: i32, pub likes: i32, pub title: String, pub author: String,}impl Book { // 2. Define the `new` associated function pub fn new(title: &str, author: &str, year: i32) -> Self { Self { title: title.to_string(), author: author.to_string(), year, likes: 0, } }}
pub struct Book { pub title: String, pub author: String, pub year: i32, pub likes: i32,}impl Book { pub fn new(title: &str, author: &str, year: i32) -> Self { Self { title: title.to_string(), author: author.to_string(), year, likes: 0, } }}
pub struct Book { // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields pub title: String, pub author: String, pub year: i32, pub likes: i32,}impl Book { // 2. Define the `new` associated function pub fn new(title: &str, author: &str, year: i32) -> Self { Book { title: title.to_string(), author: author.to_string(), year, likes: 0 } }}
pub struct Book { pub title: String, pub author: String, pub year: i64, pub likes: u64,}impl Book { pub fn new(title: &str, author: &str, year: i64) -> Self { Self { title: title.to_string(), author: author.to_string(), year, likes: 0, } }}
pub struct Book { pub title: String, pub author: String, pub year: i32, pub likes: i32,}impl Book { pub fn new(title: &str, author: &str, year: i32) -> Book { Book { title: title.to_string(), author: author.to_string(), year, likes: 0, } }}
pub struct Book { pub title: String, pub author: String, pub year: i32, pub likes: u32,}impl Book { pub fn new(title: &str, author: &str, year: i32) -> Self { Self { title: title.to_string(), author: author.to_string(), year, likes: 0, } }}
pub struct Book { pub title: String, pub author: String, pub year: i64, pub likes: i64 // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields}impl Book { pub fn new(title: &str, author: &str, year: i64) -> Self { Self {title: title.to_string(), author: author.to_string(), year, likes: 0} } // 2. Define the `new` associated function}
pub struct Book { // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields pub title: String, pub author: String, pub year: i32, pub likes: u32}impl Book { // 2. Define the `new` associated function pub fn new(title: &str, author: &str, year: i32) -> Self { Self { title: title.to_string(), author: author.to_string(), year, likes: 0 } }}
pub struct Book { pub title: String, pub author: String, pub year: i32, pub likes: u32}impl Book { pub fn new(title: &str, author: &str, year: i32) -> Self { Self { title: title.to_string(), author: author.to_string(), year, likes: 0 } }}
pub struct Book { // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields pub title: String, pub author: String, pub year: i32, pub likes: i32}impl Book { // 2. Define the `new` associated function pub fn new(title: &str, author: &str, year:i32)-> Self { Book{ title:title.to_string(), author:author.to_string(), year:year, likes:0 } }}
pub struct Book { // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields pub title: String, pub author: String, pub year: i32, pub likes: u32,}impl Book { pub fn new(title: &str, author: &str, year: i32) -> Self { Book { title: title.to_string(), author: author.to_string(), year, likes: 0, } }}
pub struct Book { pub title: String, pub author: String, pub year: i32, pub likes: i32, }impl Book { pub fn new(title: &str, author: &str, year: i32) -> Book { Book { title : title.to_string(), author : author.to_string(), year : year, likes: 0, } }}
pub struct Book { pub title: String, pub author: String, pub year: i32, pub likes: i32,}impl Book { pub fn new(title: &str, author: &str, year: i32) -> Self { Self { title: title.to_string(), author: author.to_string(), year, likes: 0, } }}
pub struct Book { // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields pub title: String, pub author: String, pub year: i16, pub likes: i32,}impl Book { // 2. Define the `new` associated function pub fn new(title: &str, author: &str, year: i16) -> Self { Self{ title: title.to_string(), author: author.to_string(), year, likes: 0 } }}
pub struct Book { pub title: String, pub author: String, pub year: i32, pub likes: i32}impl Book { pub fn new(title: &str, author: &str, year: i32) -> Book { Book {title: title.to_string(), author: author.to_string(), year, likes: 0} }}
pub struct Book { pub title: String, pub author: String, pub year: i32, pub likes: u8,}impl Book { pub fn new(title: &str, author: &str, year: i32) -> Self { Self { title: title.to_string(), author: author.to_string(), year, likes: 0 } }}
pub struct Book { // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields pub title: String, pub author: String, pub year: i32, pub likes: i32}impl Book { // 2. Define the `new` associated function pub fn new(title: &str, author: &str, year: i32) -> Book { Book { title: title.to_string(), author: author.to_string(), year, likes: 0 } }}
pub struct Book { // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields pub title: String, pub author: String, pub year: i16, pub likes: u32}impl Book { // 2. Define the `new` associated function pub fn new(title: &str, author: &str, year: i16) -> Book { Book { title: title.to_string(), author: author.to_string(), year, likes: 0 } }}
pub struct Book { pub title: String, pub author: String, pub year: i32, pub likes: u32}impl Book { pub fn new(title: &str, author: &str, year: i32) -> Book { Book { title: title.to_string(), author: author.to_string(), year, likes: 0 } }}
pub struct Book { // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields pub title: String, pub author: String, pub year: i32, pub likes: i32,}impl Book { pub fn new(title: &str, author: &str, year: i32) -> Book { Book { title: title.to_string(), author: author.to_string(), year, likes: 0 } }}
pub struct Book { // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields pub title: String, pub author: String, pub year: i32, pub likes: u32,}impl Book { // 2. Define the `new` associated function pub fn new(title: &str, author: &str, year: i32) -> Self { Self { title: title.to_string(), author: author.to_string(), year: year, likes: 0 } }}
pub struct Book { // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields pub title: String, pub author: String, pub year: i32, pub likes: u32,}impl Book { // 2. Define the `new` associated function pub fn new(title: &str, author: &str, year: i32) -> Book { Book { title: title.to_string(), author: author.to_string(), year, likes: 0, } }}
pub struct Book { // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields pub title: String, pub author: String, pub year: i16, pub likes: u16,}impl Book { // 2. Define the `new` associated function pub fn new(title: &str, author: &str, year: i16) -> Book { Self { title: title.to_string(), author: author.to_string(), year: year, likes: 0, } }}
pub struct Book { pub title: String, pub author: String, pub year: i32, pub likes: u32,}impl Book { pub fn new(title: &str, author: &str, year: i32) -> Self { Self { title: title.to_string(), author: author.to_string(), year: year, likes: 0, } }}
pub struct Book { // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields pub title: String, pub author: String, pub year: i32, pub likes: i32}impl Book { // 2. Define the `new` associated function pub fn new(title: &str, author: &str, year: i32) -> Book { Book { title: title.to_string(), author: author.to_string(), year: year, likes: 0 } }}
pub struct Book { // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields pub title: String, pub author: String, pub year: i32, pub likes: i32}impl Book { // 2. Define the `new` associated function pub fn new(title: &str, author: &str, year: i32) -> Self { Self { title: title.into(), author: author.into(), year, likes: 0 } }}
pub struct Book { // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields pub title: String, pub author: String, pub year: i32, pub likes: i32,}impl Book { // 2. Define the `new` associated function pub fn new(title: &str, author: &str, year: i32) -> Self { Book { title: title.to_string(), author: author.to_string(), year, likes: 0} }}
pub struct Book { pub title: String, pub author: String, pub year: i32, pub likes: u32,}impl Book { pub fn new (title: &str, author: &str, year: i32) -> Book { Book { title: title.to_owned(), author: author.to_owned(), year, likes: 0u32, } }}
pub struct Book { pub title: String, pub author: String, pub year: i32, pub likes: i32}impl Book { pub fn new(title: &str, author: &str, year: i32) -> Self { Self { title: title.to_string(), author: author.to_string(), year, likes: 0 } }}
pub struct Book { pub title: String, pub author: String, pub year: i32, pub likes: u32,}impl Book { pub fn new(title: &str, author: &str, year: i32) -> Self { Book { title: title.to_string(), author: author.to_string(), year, likes: 0, } }}
pub struct Book { // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields pub title: String, pub author: String, pub year: i32, pub likes: u32,}impl Book { // 2. Define the `new` associated function pub fn new(title: &str, author: &str, year: i32) -> Book { Book{title: title.to_string(), author: author.to_string(), year, likes: 0} }}
pub struct Book { // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields pub title: String, pub author: String, pub year: i32, pub likes: i32}impl Book { // 2. Define the `new` associated function pub fn new(title: &str, author: &str, year: i32) -> Book { Book { title: title.to_string(), author: author.to_string(), year: year, likes: 0 } }}
pub struct Book { // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields pub title: String, pub author: String, pub year: i32, pub likes: i32,}impl Book { // 2. Define the `new` associated function pub fn new(title: impl Into<String>, author: impl Into<String>, year: i32) -> Book { Self { title: title.into(), author: author.into(), year, likes: 0, } }}
pub struct Book { // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields pub title: String, pub author: String, pub year: i32, pub likes: i32,}impl Book { // 2. Define the `new` associated function pub fn new(title: &str, author: &str, year: i32) -> Book { Book { title: title.to_string(), author: author.to_string(), year, likes:0, } }}
pub struct Book { // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields pub title: String, pub author: String, pub year: i32, pub likes: i32}impl Book { pub fn new(title: &str, author: &str, year: i32) -> Book{ Book{ title: title.to_string(), author: author.to_string(), year, likes : 0, } }}
pub struct Book { // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields pub title: String, pub author: String, pub year: i32, pub likes: i32,}impl Book { // 2. Define the `new` associated function pub fn new(title: &str, author: &str, year: i32) -> Book { return Book { title: title.to_string(), author: author.to_string(), year: year, likes: 0 } }}
pub struct Book { pub title: String, pub author: String, pub year: i32, pub likes: i32, // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields}impl Book { pub fn new(title: &str, author: &str, year: i32) -> Self{ Self { title: title.to_string(), author: author.to_string(), year, likes:0, } } // 2. Define the `new` associated function}
pub struct Book { pub title: String, pub author: String, pub year: i32, pub likes: u32,}impl Book { pub fn new(title: &str, author: &str, year: i32) -> Self { Self { title: title.into(), author: author.into(), year, likes: 0} }}
pub struct Book { // 1. Define the fields of the struct // Make all of them public with `pub` // Read the description for the fields pub title: String, pub author: String, pub year: i32, pub likes: u32,}impl Book { // 2. Define the `new` associated function pub fn new(title: &str, author: &str, year: i32) -> Self { Self { title: title.to_string(), author: author.to_string(), year, likes: 0, } }}