Till now, I knew only one way of creating instance of struct
in Rust , which is mentioned in Rust’s official programming book, by adding curly bracket after the name of the struct and add key-value pairs inside curly brackets like below example.
struct Book {
title: String,
author: String,
genres: String,
pages: u32,
}
let book = Book {
title: String::from("Hey "),
author: String::from("abc"),
genres: "Fiction".to_string(),
pages: 150,
};
I explored another way of doing this using derive-new
. This will add an impl fn new(…) -> Self
method generated from the structs attributes that will create instance of struct.
Let’s create above struct Book
using derive-new
. To use this,
- Add dependency in
Cargo.toml
[dependencies]
derive-new = "0.5"
- Include the macro
#[macro_use]
extern crate derive_new;
#[derive(new)]
struct Book {
title: String,
author: String,
genres: String,
pages: u32,
}
The #[derive(new)]
line invokes a procedural macro in that crate, which generates code like:
impl Book {
fn new(title_value: String, author_value: String, genres_value: String, pages_value: u32) -> Bar {
Book { title: title_value, author: author_value, genres: genres_value, pages: pages_value }
}
}
You can create instance of Book
.
#[test]
fn test_book() {
let common_book = Book::new(
String::from("Common"),
String::new(),
String::from("Programming"),
100,
);
assert_eq!(
common_book,
Book {
title: String::from("Common"),
author: String::new(),
genres: "Programming".to_string(),
pages: 100
}
);
}
Since I am from Scala background and I think of Rust’s struct
as Scala’s case class
, so another advantage of using #[derive(new)]
is that we can set default values for struct variables. Default values can be specified either using #[new(default)]
attribute which removes the argument from the constructor or #[new(value = "..")]
. Let’s see an example.
#[macro_use]
extern crate derive_new;
#[derive(Debug, new, PartialEq)]
struct Book {
title: String,
#[new(default)]
author: String,
#[new(value = r#""Programming".to_string()"#)]
genres: String,
#[new(value = "100")]
pages: u32,
}
#[test]
fn test_book() {
let common_book = Book::new(String::from("Common"));
assert_eq!(
common_book,
Book {
title: String::from("Common"),
author: String::new(),
genres: "Programming".to_string(),
pages: 100
}
);
}
Now all those arguments(for ex: author, geners, pages), whose values have been set as default, will be removed from new
function. You can create instance of Book
only from title, as you can see in above example.
Now what will happen, if you pass all parameters in new
function. Let’s see
let another_book = Book::new(String::from("Hey"), String::from("abc"), "Fiction".to_string(), 150);
You will get a compilation error.
error[E0061]: this function takes 1 parameter but 4 parameters were supplied
--> src/main.rs:51:24
|
4 | #[derive(Debug, new, PartialEq)]
| --- defined here
...
51 | let another_book = Book::new(String::from("Hey"), String::from("abc"), "Fiction".to_string(), 150);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected 1 parameter
error: aborting due to previous error
For more information about this error, try `rustc --explain E0061`.
error: Could not compile `variables`.
You can now use traditional way, if you want to create Book
instance with different parameters.
let another_book = Book {
title: String::from("Hey "),
author: String::from("abc"),
genres: "Fiction".to_string(),
pages: 150,
};
derive-new
is helpful when you want to create struct
instance with default values. I hope you enjoy reading this blog. Thanks !
