error[E0495]: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements

i have the following problem:

error[E0495]: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements
   --> src/main.rs:109:25
    |
109 |             reload_list(&conn_clone, &content_clone, &combo_clone.active_id().unwrap().to_string())
    |                         ^^^^^^^^^^^
    |
note: first, the lifetime cannot outlive the lifetime `'_` as defined on the body at 108:31...
   --> src/main.rs:108:31
    |
108 |         combo.connect_changed(move |_| {
    |                               ^^^^^^^^
note: ...so that closure can access `conn_clone`
   --> src/main.rs:109:25
    |
109 |             reload_list(&conn_clone, &content_clone, &combo_clone.active_id().unwrap().to_string())
    |                         ^^^^^^^^^^^
    = note: but, the lifetime must be valid for the static lifetime...
note: ...so that reference does not outlive borrowed content
   --> src/main.rs:109:25
    |
109 |             reload_list(&conn_clone, &content_clone, &combo_clone.active_id().unwrap().to_string())
    |                         ^^^^^^^^^^^
...

The code is around:

...
fn main() -> Result<()> {
        ...
        reload_list(&conn, &content, &combo.active_id().unwrap().to_string());

        combo.connect_changed(move |_| {
            reload_list(&conn_clone, &content_clone, &combo_clone.active_id().unwrap().to_string())
        });
        ...
}
...
fn reload_list(conn: &'static Connection, content: &'static ListBox, list_id: &'static String) {
        ...
        save_edit_button.connect_clicked(move |_| {
                ...
                reload_list(&conn, &content, &list_id);
        });
        ...
}

So how can I fix it, that this code will run. I had added 'static to the reload_list parameters, as the compiler said it, so this can be removed, but it comes: explicit lifetime required in the type of conn, content, list_id

I'm assuming you need the .connect_changed(…) and .connect_clicked(…) closures to be 'static, i.e., guaranteed not to dangle while owned.

They thus cannot use local borrows, since those will not be static (i.e., &'static … is rarely the right type to use).

Instead, the true "'static reference" type is not &'static Foo, but Arc<Foo>:

let conn = Arc::new(conn);
…
combo.connect_changed({
    let conn = Arc::clone(&conn);
    let content = Arc::clone(&content);
    let list_id = Arc::clone(&list_id);
    move |_| {
        …
        reload_list(conn, content, list_id);
        …
    }
});

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.