Borrow Might Be Used Here (at end of main fn)

⚓ rust    📅 2025-05-30    👤 surdeus    👁️ 3      

surdeus

I am having some trouble with lifetimes it seems.

The compiler is saying my app which takes a lifetime won't live long enough, but I'm scratching my head. The compiler says it might be borrowed at the end of main when app is dropped but I clearly am not using it.

The code:

// In app module
pub struct App<'app> {
    event_handler: EventHandler<'app>,
    state: Rc<RefCell<State>>,
}

// In main
fn main() -> io::Result<()> {
    let mut terminal = ratatui::init();

    let mut app = App::init();

    ratatui::restore();

    app.run(&mut terminal) // returns result
}

Error when running:

error[E0597]: `app` does not live long enough
  --> src/main.rs:12:5
   |
8  |     let mut app = App::init();
   |         ------- binding `app` declared here
...
12 |     app.run(&mut terminal)
   |     ^^^ borrowed value does not live long enough
13 | }
   | -
   | |
   | `app` dropped here while still borrowed
   | borrow might be used here, when `app` is dropped and runs the destructor for type `App<'_>`

I tried creating a let binding:

fn main() -> io::Result<()> {
    let mut terminal = ratatui::init();

    let mut app = App::init();

    ratatui::restore();

    let result = app.run(&mut terminal);

    result
}

But I get this error:

error[E0597]: `app` does not live long enough
  --> src/main.rs:12:18
   |
8  |     let mut app = App::init();
   |         ------- binding `app` declared here
...
12 |     let result = app.run(&mut terminal);
   |                  ^^^ borrowed value does not live long enough
...
15 | }
   | -
   | |
   | `app` dropped here while still borrowed
   | borrow might be used here, when `app` is dropped and runs the destructor for type `App<'_>`

5 posts - 3 participants

Read full topic

🏷️ rust_feed