Rust tips is a user on octodon.social. You can follow them or interact with them if you have an account anywhere in the fediverse. If you don't, you can sign up here.
Rust tips @rust

Generic value in a struct doesn't live long enough?

If you want a struct that explicitly holds a reference to some generic type, this won't be enough:

struct Foo<T>(&T);

requires not only explicit lifetime:

struct Foo<'a, T>(&'a T);

but also specifying that any lifetimes hidden in the type `T` outlive it:

struct Foo<'a, T: 'a>(&'a T);

BTW: `struct Foo<T>(T)` can hold references too (since *any* type includes reference types, too).

· Web · 1 · 2