Most idiomatic way to create a default struct

半城伤御伤魂 提交于 2020-06-11 18:09:58

问题


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


来源:https://stackoverflow.com/questions/41510424/most-idiomatic-way-to-create-a-default-struct

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!