Can't Rust infer what traits to derive from? (Clone,Copy etc)


Hi, rustc gives me pretty conclusive errors when, e.g. I have to derive a Clone, Copy etc.
Could the compiler not automatically infer whether to derive a struct/enum from a certain trait?



The problem comes when you don’t want to implement the trait. You may just have slipped and used something in the wrong place.


True, I’m pretty sure that rust could auto infer, but this seems like a very plausible reason not to :smiley:


I suspect that it would be pretty hard to infer someone’s intentions from their code, but who knows what the future holds? It’s better to err on the safe side, though, until computers becomes better at reading minds. :wink:


It can infer that you have to implement core::clone::Copy for something that’s inside something else that does. If and outer struct implements Copy so needs the inner. But still, you might want to implement copy differently.


Yeah, it’s complicated, but it would be very nice to have sometimes :smile:


-1 for duck typing. Rust tries to be explicit where it counts, and even if you have not encountered them, there are cases where it would mess things up.


The main reason for this afaik is that it is a breaking change to remove one of these traits. If you have a struct which only stores Copy types at the moment, but you know in the future you might add a non-Copy type, you don’t want rust to infer Copy, and then break all usages of that type being Copy in the future.

The opt-in method currently used allows you to backwards-compatibly add non-Copy data to a struct or enum which previously only held Copy data.