Missing Lifetime Parameter

Hey there. I have a struct that looks like so:

struct TestEntity{
    position: render_object::Position,
    texture: sdl2::render::Texture,
    game_context_ref: game_context::GameContext,
    game_canvas_rev: sdl2::render::Canvas<sdl2::video::Window>,

The documentation for the Texture object is here:

I receive the error:

error[E0106]: missing lifetime specifier
 --> src\entities\test_entity.rs:7:14
7 |     texture: sdl2::render::Texture,
  |              ^^^^^^^^^^^^^^^^^^^^^ expected lifetime parameter

error: aborting due to previous error

I’m not quite sure why a lifetime specifier is required here, or what it’s even looking for with respect to one. As I understand it, lifetime specifiers are required for references, but this is the declaration for the field of a struct. Why is it needed and how do I satisfy the compiler’s requirement, here?

Texture has a lifetime parameter, because it stores a reference. So you must propagate the lifetime.

This is similar to

struct Foo<T> { t: T }

struct Bar { foo: Foo }

This is wrong because we have a type parameter on Foo, but we don’t give it a value inside of Bar.

For the same reason

struct TestEntity { texture: sdl2::render::Texture }

is wrong, because we have a lifetime parameter on Texture that we don’t give a value to.

1 Like

So the TestEntity struct would need to have a parameter that is then passed to Texture?

Yes, that is correct

Perfect. I feel that I better understand lifetimes in Rust. Thank you!

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.