I tried to explain the
'static bound to a friend, since it's one of those concept that can seem a bit confusing. I'm not a PL expert so I thought I'd check out if you think the explanation I gave is a good one or if you have a better way of explaining it?
When you see something requiring
'static, it doesn't really mean it has to be static like
&'static strwhich are placed in the .bss or .data segment of the executable file (and therefore exists for the liftetime of the program). It basically means that the the lifetime of the object isn't dependant on anything else and therefore is not restricted by any other part of the program.
Stringas an example. If we take a reference to a string
&String, or a reference to the a slice of the string
&str,these references will always depend on the lifetime of the
Stringobject they're pointing to and will not satisfy a
'staticbound. If you pass in the
Stringobject itself, it will satisfy the
'staticrequirement even tough it might be dropped before the program ends. The special case here is
&static strwhich points to an "object" which is guaranteed to live the entire lifetime of the program so it's fine to reference it.
'staticis a requirement that affects references. It's important to remember that not all pointers are references. If you pass in a
Box<String>it will be a pointer just like
&Stringbut since the
Stringit points to is heap allocated we know by the guarantees
Boxgives us that the lifetime of the
Stringobject is tied to the liftime of the Box-pointer and not to a stack frame like a "normal"
Do you know of a better explanation that you want to share (or spot any mistakes in mine)?