Skip to main content


#Rust help requested. I am officially at wits end here.

Please take a look at my source file sdlstate.rs.

I'm getting a borrow checker error E0597 on line 39, indicating that tc does not live long enough. However, looking at the sdl2 sources (via its rust-docs), I don't see how my created texture, t, can possibly still refer to tc, which as far as I'm aware, is the only way this error can be generated under current conditions.

error[E0597]: `tc` does not live long enough<br>  --> src/sdlstate.rs:39:21<br>   |<br>14 |   impl<'a> SdlState<'a> {<br>   |        -- lifetime `'a` defined here<br>...<br>39 |           let mut t = tc<br>   |  _____________________^<br>40 | |             .create_texture(<br>41 | |                 Some(sdl2::pixels::PixelFormatEnum::RGBA8888),<br>42 | |                 sdl2::render::TextureAccess::Streaming,<br>43 | |                 self.width,<br>44 | |                 self.height,<br>45 | |             )<br>   | |_____________^ borrowed value does not live long enough<br>...<br>49 |           self.current_texture.set(Some(t));<br>   |           --------------------------------- argument requires that `tc` is borrowed for `'a`<br>...<br>56 |       }<br>   |       - `tc` dropped here while still borrowed<br><br>

Why is this happening? Why can't I re-arrange the code to prevent this from happening?

Right now, the only way this code will compile and run correctly is if I manually inject the re-paint code where I invoke f(), which utterly defeats the purpose and benefit of using closures in the first place.

In an attempt to fix this, I've tried:

  • Replacing the Cell with RefCell.
  • Removing Cell all-together and just using a raw Option type.
  • Removing the 'a lifetime annotation.

None of these work, and almost always introduce some manner of errors on their own.

Please help. Thanks.

#rust