struct App<'a> {
data: Vec<i64>,
ui: UI<'a>
}
struct UI<'a> {
ui_data: Option<&'a Vec<i64>>,
// other fields
}
impl UI<'_> {
pub fn draw(self) {
// do something with ui_data
self.ui_data;
}
}
impl<'a> App<'a> {
pub fn new() -> Self {
let data = vec![1,2,3,4];
App {
data,
ui: UI {
ui_data: None
}
}
}
pub fn post_init(&'a mut self) {
self.ui.ui_data = Some(&self.data);
}
}
fn func(app: App) {
app.ui.draw()
}
pub fn main() {
let mut app = App::new();
app.post_init();
func(app);
}
The compiler complains
error[E0505]: cannot move out of `app` because it is borrowed
--> src/main.rs:29:10
|
26 | let mut app = App::new();
| ------- binding `app` declared here
27 | app.post_init();
| --- borrow of `app` occurs here
28 |
29 | func(app);
| ^^^
| |
| move out of `app` occurs here
| borrow later used here
Maybe making func(app)
to func(&app)
is a solution. But what if I don't want to use this solution?