'static trait on impl

If I have an impl like so:

struct Foo {}
impl<E> Foo<E>
where E: Sync + Send + 'static {

fn new() -> Foo {

Every-time I call Foo::new() does it stick around in 'static and never freed?

No, of course it doesn't leak. That is not what 'static as a bound (or any other lifetime bound) means.

'static as a bound means that your type would be valid to put in a 'static or create a &'static reference to, if you wanted to. (Ie., practically, it contains no short-lived references.)

See this for more details.


got it, thanks

Another way to look at it is that lifetimes don't apply at all to types that don't contain references. i32: 'static is true not because i32 lives long enough, but because it ignores the lifetime bound as if it wasn't there at all.

Lifetime bounds can also never extend or shorten the lifetime of something, just like trait bounds don’t conjure trait impls for types that don’t have them.

1 Like

I think the misunderstanding goes deeper, in some sense.

The very first thing you have to realize is the fact that both types and values have lifetimes and there no direct match.

Value can not outlive it's type, for obvious reasons, but parts of it can do that easily. Consider the following type:

struct Pair<'a> {
    foo: &'a i32,
    bar: String,

It's short-living type, half of it is attached to some temporary object (foo refers it) but even if Pair would be destroyed and object that it was linked to is destroyed also… bar string may survive and continue to live for much longer, till the end of program execution, in fact.

And that leads to subtle, yet critical, difference: when you write &'static T you are dealing with value, but when you write T + 'static then you are dealing with type. IOW: these two statics look superficially similar, but they are applied to entities of different kinds.

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.