Hi everyone,
Stuck with another one, if someone can share a better way please.
struct SomeHolder {
len: u32,
type: String,
common_holder: Box<CommonHolder>
}
impl Default for SomeHolder {
fn default() -> Self {
SomeHolder{type: "UNKNOWN".to_string(),
..Default::default()}
}
}
warning: function cannot return without recursing
--> src/main.rs:34:5
|
34 | fn default() -> Self {
| ^^^^^^^^^^^^^^^^^^^^ cannot return without recursing
35 | BoxCommon{box_type: "XXXX".to_string(),
36 | ..Default::default()}
| ------------------ recursive call site
|
= help: a `loop` may express intention better if this is on purpose
= note: `#[warn(unconditional_recursion)]` on by default
What is the usual way of creating default values for structs? (Searching on this topic did give some results, but I have notcome across hierarchical Default chaining yet)
Initially, I thought new was in an implicit trait since majority of the standard library types seem to have implemented new. I guess not, and I had to resort to Default.
I can go with my own new for all the structs that I am defining and chain the new methods between base struct and specific structs : however, I am losing out on “And the rest” pattern binding.
I can also see how this is going to become a problem when adding/modifying struct members if I am dealing with few levels of nested and/or hierarchical structs - I'd have to revisit every single new to take care of the modification/addition.
Is there a simpler way?
Many thanks!