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.
struct Post {
pub content: String,
pub user: String,
pub views: u32
}
fn main(){
let new_blog_post = Post {
content: "Some content".to_string(),
user: "Jon Doe".to_string(),
views: 0
};
}
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.
impl Post {
pub fn new(content: String, user: String) -> Post {
Post {
content,
user,
views: 0
}
}
}
fn main(){
let new_blog_post = Post::new(
"Some content".to_string(),
"Jon Doe".to_string()
);
}
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).
let book = Book::new(
"ASOIAF",
"George R. R. Martin",
1996
);
assert_eq!(book.title, "ASOIAF");
assert_eq!(book.author, "George R. R. Martin");
assert_eq!(book.year, 1996);
assert_eq!(book.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 { // 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: 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: i16, pub likes: i16,}impl Book { pub fn new(title: &str, author: &str, year: i16) -> Self { Self { title: title.to_owned(), author: author.to_owned(), 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 { Book{ title: String::from(title), author: String::from(author), 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, 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) -> Self { Book { title: title.to_string(), author: author.to_string(), year, likes: 0, // Default value for likes } }}
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: u64}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: 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: u8,}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 { // 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: 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: i32,}impl Book { // 2. Define the `new` associated function pub fn new(title: &str, author: &str, year: i32) -> Book { 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: 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 { // 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 { 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_owned(), author: author.to_owned(), 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: usize,}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: &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: i64, pub likes: i64,}impl Book { pub fn new(title: &str, author: &str, year: i64) -> 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 { 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 { // 2. Define the `new` associated function pub fn new(title: &str, author:&str, year: i32) -> Self { return Book { title: title.to_string(), 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: i32,}impl Book { // 2. Define the `new` associated function pub fn new(title: &str, author: &str, year: i32) -> Self { Book { 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: 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: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) -> 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: i32,}impl Book { pub fn new(title: &str, author: &str, year: i32) -> Book { Book { title: title.to_owned(), author: author.to_owned(), 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: 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.into(), author: author.into(), 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 { // 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: String::from(title), author: String::from(author), 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: 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 { 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: u32}impl Book { // 2. Define the `new` associated function pub fn new(title: &str, author: &str, year:i32)-> Book { Book{ year, title:title.to_string(), author:author.to_string(), 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_owned(), author: author.to_owned(), 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 { // 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_owned(), author : author.to_owned(), 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: 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: i32,}impl Book { // 2. Define the `new` associated function pub fn new(title: &str, author: &str, year: i32) -> Self { Self { title: String::from(title), author: String::from(author), 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, 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 }; }}