Reputation: 42849
To create a default struct, I used to see fn new() -> Self
in Rust, but today, I discovered Default
. So there are two ways to create a default struct:
struct Point {
x: i32,
y: i32,
}
impl Point {
fn new() -> Self {
Point {
x: 0,
y: 0,
}
}
}
impl Default for Point {
fn default() -> Self {
Point {
x: 0,
y: 0,
}
}
}
fn main() {
let _p1 = Point::new();
let _p2: Point = Default::default();
}
What is the better / the most idiomatic way to do so?
Upvotes: 39
Views: 24623
Reputation: 11
I agree with the existing answer regarding implementing both, but will add a few extra details for posterity. You can implement Default, then add new automatically with a trait template:
pub(crate) trait New: Sized + Default {
fn new() -> Self {
Default::default()
}
} impl<T: Default> New for T { }
Typically I will use this for simple structs, but for structs with significant initialization overhead Default::default will return an empty object (eg. for filling an uninitialized array) whereas new will return a fully initialized instance.
Upvotes: 1
Reputation: 431679
If you had to pick one, implementing the Default
trait is the better choice to allow your type to be used generically in more places while the new
method is probably what a human trying to use your code directly would look for.
However, your question is a false dichotomy: you can do both, and I encourage you to do so! Of course, repeating yourself is silly, so I'd call one from the other (it doesn't really matter which way):
impl Point {
fn new() -> Self {
Default::default()
}
}
Clippy even has a lint for this exact case!
I use Default::default()
in structs that have member data structures where I might change out the implementation. For example, I might be currently using a HashMap
but want to switch to a BTreeMap
. Using Default::default
gives me one less place to change.
In this particular case, you can even derive Default
, making it very succinct:
#[derive(Default)]
struct Point {
x: i32,
y: i32,
}
impl Point {
fn new() -> Self {
Default::default()
}
}
fn main() {
let _p1 = Point::new();
let _p2: Point = Default::default();
}
Upvotes: 44