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).