The E0310 error description states that there are instances when a type lifetime must be defined. But shouldn't the type lifetime always be static? How can a type disappear from a program?
: 'static annotation means something different – "a value of this type does not contain any short-lived references".
So, a few examples:
i32– fine, integers don't have any references
Vec<u32>(and other collections) – fine, vector contains a pointer to a heap-allocated storage, but it's not a reference, since it's fully owned by the vector.
&'static str(such as
"hello") – fine, it's a reference, but the reference is valid for the whole lifetime of a program.
let x = 5; let r: &i32 = &x) – here the type of
rdoes not satisfy
rcontains short-lived reference to a variable
- any struct that contains such a reference is not
So basically by saying
T: 'static you restrict the
T to types that don't contain any references (except of
I see. Thank you.