Builder+Structopt

#[derive(Debug, Clone, Builder, StructOpt)]
//#[builder(build_fn(validate = "Self::validate_parameters"))]
pub struct FileParametres{
    #[builder(public)]
    pub equation_type:i8,
    #[builder(default = "(0 as f32, 1 as f32)")]
    pub margin_domain:(f32,f32),
    add_args: (Option<TypeTsk>, Option<i8>, Option<bool>)//will be last background_mc additional_correction
}

Please, discribe me anerror and how can avoid it (it will appear if Builder and StructOpt together in use)
the trait bound (f32, f32): std::str::FromStr is not satisfied

the trait std::str::FromStr is not implemented for (f32, f32)

note: required by std::str::FromStr::from_strrustc(E0277)

main.rs(30, 5): the trait std::str::FromStr is not implemented for (f32, f32)
Thanks)

derive_builder crate appears to use FromStr impls to set the "default" values. Tuples (e.g. (f32, f32) in your case) don't implement FromStr, hence the error.

Try pointing "default" at an fn, e.g.:

#[builder(default = "Self::default_margin_domain()?")]
pub margin_domain: (f32, f32)

// elsewhere in FileParameters
fn default_margin_domain() -> Result<(f32, f32), String> {
     Ok((0.0, 1.0))
}
1 Like

But there is no error without one of Builder or StructOpt, why?

Can you elaborate a bit? If you don't specify the "default" attribute, they're likely using the value's Default impl, which (f32, f32) does have (i.e. (0.0, 0.0)).

1 Like

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.