rust - Store reference of struct in other struct -
i have 2 structs. app , item . what want achieve store item in items vector of app struct passing mutable reference item s constructor. pub struct app<'a> { items: vec<&'a item> } impl<'a> app<'a> { pub fn new() -> app<'a> { app { items: vec::new() } } pub fn register_item(&mut self, item: &'a item) { self.items.push(item); } } pub struct item; impl item { pub fn new(app: &mut app) -> item { let item = item; app.register_item(&item); item } } fn main() { let mut app = app::new(); let item = item::new(&mut app);; } the code thows following error: test.rs:8:28: 8:32 error: `item` not live long enough test.rs:8 app.register_item(&item); is there way this? while rc might correct use case, it's understand why getting error are. please read why can't store value , reference value in...