0

I have two structs. The first struct, Application should have a reference to the second struct, UserInterface. The second struct should in turn contain a reference to the first struct. So, I want to somehow pass &self when instantiating the UserInterface struct on line 8. I want to do this because I need access to Application from inside UserInterface. Here's my code:

pub struct Application<'a> {
    pub user_interface: &'a UserInterface<'a>,
}

impl<'a> Application<'a> {
    pub fn new() -> Self {
        Self {
            user_interface: &UserInterface { selected: 0, app: &self }, // pass &self here somehow
            all_entries: read()
        }
    }
}

pub struct UserInterface<'a> {
    selected: u8,
    app: &'a Application<'a>
}

impl<'a> UserInterface<'a> {
    pub fn populate_screen(&self) {
        ...
    }
}

Is it possible to do this? If not, what are my options? What are the alternatives?

adder
  • 3,302
  • 12
  • 26
  • You may want to have a look at this closely related question: [Why can't I store a value and a reference to that value in the same struct?](https://stackoverflow.com/questions/32300132/why-cant-i-store-a-value-and-a-reference-to-that-value-in-the-same-struct?rq=1) – Frxstrem Apr 30 '20 at 22:58
  • @Frxstrem I had a look, but it didn't help much really. – adder Apr 30 '20 at 23:27
  • As stated, this is a duplicate. If you want you can add more details and explain why the duplicate doesn’t apply to your case and vote to reopen. – mcarton Apr 30 '20 at 23:57

0 Answers0