I write impl Default for Enum { fn default() -> Self { Enum::DefaultVariant } }
for too many times today. I would like a less repetitive code:
#[macro]
enum Enum {
#[default_variant]
DefaultVariant,
Foo,
Bar,
}
Is there a crate that provide that feature?
Probably not, but it's really easy to write this sort of custom derive yourself.
You could also propose a Default
derive for the derive_more crate, which already has a lot of these "utility" derives.
smart-default provides just what I need.
1 Like
Took me a bit to write this post, so I missed it. In case you're looking for other options, here are two more:
educe:
#[derive(Educe)]
#[educe(Default)]
enum Enum {
#[educe(Default)]
DefaultVariant,
Foo,
Bar,
}
defaults
:
#[derive(Defaults)]
#[def = "DefaultVariant"]
enum Enum {
DefaultVariant,
Foo,
Bar,
}
(and smart-default
, which you found):
#[derive(SmartDefault)]
enum Enum {
#[default]
DefaultVariant,
Foo,
Bar,
}
2 Likes
system
Closed
5
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.