Is my understanding to lifetime constraint in generic function correct?

In the declaration of a generic function, after the where clause, we may write:

where T: SomeTrait + 'a

Previously I'm confused about how could a type be associated with some lifetime, while it is not a reference.

Then I guess that T it self could be a reference, for example, T is instantiated to be e.g., &u32, then its lifetime becomes relevant.

In another example:

where T: SomeTrait + 'static

implies that eityer T has to be a &'static U (where U is some other type) , or it cannot be a reference type.

Is that correct?

1 Like

I think T: 'a actually means that any lifetime T contains must have a lifetime of 'a or longer. This of course includes the cases where T is a reference, but in general, T can be any type. Say if T is U<'x, 'y>, then it must be the case that 'x: 'a and 'y: 'a for T: 'a to be true.

2 Likes

Type might be the reference, yes, or it may also contain the reference. In the latter case, it'll have an associated lifetime, too (not larger then the one of the reference inside it).

1 Like

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