Confused about type lifetimes and E0310

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.
  • &i32 (such as let x = 5; let r: &i32 = &x) – here the type of r does not satisfy 'static, because r contains short-lived reference to a variable x.
  • any struct that contains such a reference is not 'static also.

So basically by saying T: 'static you restrict the T to types that don't contain any references (except of 'static references).


I see. Thank you.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.