#[derive(...)] practices

I'm working through my first rust project over here. In that code, I have this struct:

#[derive(Debug, Clone, PartialEq, Eq)]
struct Meta {
    // Every field in Meta is optional
    title: Option<String>,
    passwords: Vec<String>,
    tags: Vec<String>,
    category: Option<String>,
}

My question is how do you decide what traits to implement? My own reasoning for each was that I can't see any reason why I shouldn't. Being able to print the object, clone it, or do equality checks all seems like very helpful qualities for any struct. I'm sure there's more use case specific traits but to me a few (like the ones I've used) seem like something that'll be helpful in a vast majority of cases. I'm curious to hear about the idiomatic approach to deciding what traits one should implement.

You should check the official API guidelines about common traits for public types: Interoperability - Rust API Guidelines

For private types, I'd say use whatever is required by your project.

2 Likes

Thank you, that pretty much covers exactly what I wanted to know.