Indicate that value has static lifetime

let left_name: &'static str = "LeftName";`
siv.add_global_callback(event :: Key :: F5, |siv_X| {
        siv_X.call_on_name(
            &left_name,
        );
    });

The error I'm getting:
|siv_X|
^^^ may outlive borrowed value left_name
But this is impossible, as left_name has static lifetime. How to indicate in the closure that the left_name has static lifetime?

It doesn't, it's a local variable. It happens to be a reference that points to a 'static str, but it's not 'static itself.

You should therefore not take its address, likely. Try siv_X.call_on_name(left_name) instead.

If that does not work, because call_on_name actually requires a &&str (as opposed to just a &str, which I highly doubt), then declare left_name as static LEFT_NAME: &str = "LeftName"; instead.

2 Likes

Got it, thanks for the explanation.

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.