What does <'_> Mean?

I encountered this in the docs. What does <'_> mean in this context?

pub fn lines(&self) -> Lines<'_>

Normally you denote lifetimes as 'a. '_ is the notation for saying that the lifetime is "elided", ie, the compiler infers the lifetime.

1 Like

Normally you use the angle brackets (< and >) when something is generic over something else. For example, Vec<T> means the Vec type is generic over some type, T, and Vec<u8> means we're telling the compiler "hey, I want you to substitute T = u8".

As well as types, you can also be generic over lifetimes. So Lines<'src> is valid syntax and says we've got a Lines object which must be alive for some lifetime 'src (I made up the name, but '_ or 'a are equally valid syntax).

I'm doing a bit of hand-waving here, but that's usually enough to start building intuition. If I wanted to be a bit more technical when describing Lines<'_>, I would say Lines is a "type constructor" (i.e. the scaffold/template for a type) and we "instantiated" it (got a concrete type) by substituting in the anonymous lifetime '_ .

1 Like

See this page of the nomicon for a quick overview of lifetime elision

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.