Why does the Lifetime syntax use in 'a instead of '0 or more any number?

This seems counter-intuitive, and I find such syntax a bit ugly, As if I could name it.

It's just a convention. You can name lifetimes however you want.


I don't know. Things have names in high level languages. After all we use variable names and function names instead of using their addresses in memory. Using numbers might imply some ordering of lifetimes which is not appropriate.


You can name it, like you can generic types, and sometimes people do. Main downside is the space it takes in signatures.

There was a push to lint against single letter lifetimes 5 years ago or so, but it didn't gather any momentum.


Personally I find all generic and lifetime syntax cluttering up struct definitions and such ugly. It all starts to look like "line noise" line noise - Wiktionary, the free dictionary which my mind struggles to make any sense of. But hey, that's down to familiarity and me. I don't have any suggestions for a better way to do it.

But it's not clear to me how using numbers instead of letters helps make things less ugly or more intuitive.

We can number them if we want:

struct BorrowedThings<'l1, 'l2, 'l3> {
    x: &'l1 i32,
    y: &'l2 i32,
    l: &'l3 i32,

Hmm... except stupid modern day fonts often make l look pretty much the same as 1 and I which is very annoying.

1 Like

For a standard library example of informatively named lifetimes, consider std::thread::scope(), which uses two lifetimes 'env and 'scope, representing different things as explained in the documentation. Giving them helpful names helps to tell them apart (especially since the names are used not only in the function but also across the Scope struct). Of course, giving informative lifetime names matters a lot less when only one lifetime is annotated, so the name 'a is still particularly common in the single lifetime case.


It's something that behaves nominally, so it following the same naming rules as other identifiers seems like a very reasonable choice, to me.

'a is basically the lifetime-generics version of T in type-generics or of i as a loop iteration variable.

Yes, it has the extra lexical marker of the ' so it could support a wider space, but "work like the other things that are hooked up nominally" is a good default, so it'd need more than just "I like '0 better than 'a" to change it.

(I don't think anyone particularly loves rust's lifetime syntax -- particularly not '_ -- but it's fine. And Rust has put a bunch of work into usually not needing to name lifetimes. Even things like impl Debug for TakesLifetime<'_> { … } works now, no need to name that lifetime since it won't be mentioned again later in the impl.)


rust-analyzer elided lifetime inlay hints do use '0, '1, etc.


This is very convenient because it doesn't clash with lifetime annotations in the source.


That's a good point, actually -- the NLL borrow-check errors also use '1 and such to give names to elided lifetimes, so not supporting numbers in user-facing syntax gives a nice clear choice for those to use in the explanations that can't conflict with user-written ones.

EDIT: which is what @tbfleming was typing at the same time, lol :upside_down_face:


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.