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?
Default values can be assigned to a struct by using a constructor function. Rather than creating a structure directly, we can use a constructor to assign custom default values to all or some of its members. Another way of assigning default values to structs is by using tags.
For variables of class types and other reference types, this default value is null . However, since structs are value types that cannot be null , the default value of a struct is the value produced by setting all value type fields to their default value and all reference type fields to null .
The default zero value of a struct has all its fields zeroed.
struct foo { foo() : a(true), b(true) {} bool a; bool b; bool c; } bar; Am I correct to assume I would end up with one struct item called bar with elements bar. a = true , bar. b = true and an undefined bar.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With