Most idiomatic way to create a default struct

2020-08-17 07:42发布

问题:

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?

回答1:

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();
}


标签: rust