idanarye / rust-smart-default

Rust macro for automatically generating default
MIT License
133 stars 7 forks source link

Support for defaults for Vec<String> #8

Open apps4uco opened 2 years ago

apps4uco commented 2 years ago

Hi,

Im not sure if it is supported, but in any case it would be useful to document

I tried

#[default(_code = "vec![\"en\"]")]
knows_language: Vec<String>,

and

#[default(_code = r#"vec!["en"]"#)]
knows_language: Vec<String>,

and

#[default(_code = "vec![r#"es"#]")]
knows_language: Vec<String>,
idanarye commented 2 years ago

You probably don't even have to use _code. This works:

#[default(vec!["en".to_owned()])]
knows_language: Vec<String>,

The reason your code didn't work is that "en" is not a String - it's an &str. The compiler error message does tell you that. I do see a problem with _code here - when you use it it points you at the #[derive] attribute:

1 | #[derive(SmartDefault)]                                                                
  |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^- help: try using a conversion method: `.to_string()`             
  |          |                                                                                            
  |          expected struct `String`, found `&str`

While when you don't use it it points you at the correct place:

9 |     #[default(vec!["en"])]                                                                            
  |                    ^^^^- help: try using a conversion method: `.to_string()`                          
  |                    |                                                                                  
  |                    expected struct `String`, found `&str`

So this is one thing I should probably fix.