How to fix error[E0521]: borrowed data escapes outside of method?

Hi folks !

I need to fix the issue:

error[E0521]: borrowed data escapes outside of method
   --> src/
131 | impl<'a> UI<'a> {
    |      -- lifetime `'a` defined here
140 |     fn handle_events(&mut self) -> JoinHandle<()> {
    |                      --------- `self` is a reference that is only valid in the method body
143 |         let characters_modal = Arc::clone(&self._characters_modal);
    |                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |                                |
    |                                `self` escapes the method body here
    |                                argument requires that `'a` must outlive `'static`

I created a minimal sandbox example to reproduce this. Actually it's cropped version of my actual code.

Could somebody help on that to explain why this happens and how to fix this ? I believe "static" lifetime here is not a good choice and should be avoided.

spawn_blocking requires the 'static.

1 Like

Actually I am rewriting the part of my application and I used spawn_blocking before, but didn't set static lifetime. For example, here: tentacli/src/client/ at primary · idewave/tentacli · GitHub

How it works in that case ?

EventIncome has no lifetime (or type) parameters and thus satisfies a 'static bound; moreover you don't capture &mut self in the closure. Thus the closure satisifes a 'static bound.

1 Like

So, the only way to fix my issue is to set static lifetime for UI ?

Probably that, or have a method on all your lifetime-carrying structs that recursively drills down to the Cows and makes them Strings or Cow<'static, str>s on demand (within the method body).

Or find some framework that doesn't require 'static.

1 Like

Thank you very much for this hint ! I refactored the part of application where lifetimes were used and removed them. So now that error gone.

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.