I am developing a personal project using Rust, but currently I am having troubles to create and return an object in which one of the members has a reference its parent / container.
Here is a simplified version of the code :
struct Parent<'a> {
child: Option<Child<'a>>,
}
struct Child<'a> {
parent: &'a Parent<'a>,
}
fn get_object<'a>() -> Parent<'a> {
let mut object = Parent {
child: None,
};
object.child = Some(Child {
parent: &object,
});
return object;
}
I tried different tweaks to make this code compile, changing the code structure and lifetimes but ultimately it did not work, and I get different error messages depending on how I try it.
I would really appreciate some help with this, thank you for reading.