Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I think lifetimes are best thought of as constraints.

Like `x: Foo<'a>` means "x is constrained by lifetime 'a" as in "x is only guaranteed to be soundly usable if 'a is 'alive'".

With the implicit context of "you are only allowed to use things which are guaranteed to be soundly usable".

And that "moving" is distinct from lifetimes (i.e. if you move x out of scope you just don't have it anymore, while a 'a constraint on x limits where you _can_ "have" it).

Then `'static` basically means "no constraint"/"unconstrained".

So a `x: X + 'static` mean x has no constraints, as long as you have it you can use it.

Where `x: X+'a` (or `X<'a>`) would mean you can _at most_ have x until 'a stops being "alive". (This doesn't mean the data behind X doesn't life longer, just that in that specific place you can't "have" it at most while 'a is alive).

So then if we look at `&'static A` we have a type which 1) is always copied instead of moved, so you always have it while you can have it and 2) you can always have it. As consequence it must life for the whole program execution. Not because `'static` says "until end of program" but because a unconstrained &reference can only be sound if it exist until the end of the program!!



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: