the `#[default]` attribute may only be used on unit enum variants
consider a manual implementation of `Default`rustcClick for full compiler diagnostic
core::default
derive_helper default
I think it should be possible to use field string default like I did. Just set it to an empty string value like you would when doing String::default()
Ok, I just wanted to make sure I didn't request too much at once. If the above feature is already implemented then here is the thing I actually wanted to do:
Currently, since the variadic parameter is not supported yet, we cannot impl a function that accept different parameter without some hacks.
The best choice is that
pub struct Structer {
pub stringy: String,
}
// method 1, define the custom default function, then Default::default for Structer will generate stringy with "ok" as default.
impl Default for Structer {
fn default()->Self {Self{stringy:"ok".to_string()}}
}
// method 2, impl a new default function, you could then call Structer::new to generate your default struct
impl Structer {
fn new()->Self {Self{stringy:"ok".to_string()}}
}
This is not a thing that derives are allowed to do. All the derive sees is the literal tokens that make up the type definition. So if it sees String, that could be any type, even &'static str. Or the type could be Foo, and that's sometimes a type alias for String.
It's awkward since it requires writing Rust code in a string literal, but it does support arbitrary expressions, e.g.
#[derivative(Default(value="\"ok\".into()"))]
More directly applicable to your usage is smart_default, which allows writing: