I have issue with lifetimes and references. I have three structs:
struct Foo<'a> {
bar: Bar,
lar: Lar<'a>
}
struct Bar;
struct Lar<'a> {
bar: &'a Bar
}
I want to create the final structure like this:
Foo {
bar: Bar::new(),
lar: Lar::new(&bar); // use the reference to bar created above
}
I tried two approaches: 1.
fn main() {
let bar = Bar::new();
Foo {
bar,
lar: Lar::new(&bar) // at this point the scope of bar is still just the function block,
// I get: returns a value referencing data owned by the current function
}
}
fn main() {
let mut foo = Foo {
bar: Bar::new(),
..Default::default() // even if I use @[derive(Default)] on all the structs,
//I still get: the trait `std::default::Default` is not implemented for `&Bar`,
//I think the problem is that its a referrence not the actuall structure
};
foo.lar = Lar::new(&foo.bar);
foo
}
How would I solve this? Thank you