Now that I made myself a little bit familiar with the basic concept of lifetimes in Rust I'm facing a different problem. I'm having a pretty hard time to wrap my head around the lifetime syntax.
Is there a general rule when to use which syntax for lifetime annotations in Rust? It seems to me there are so many variations on the syntax and I always have to poke around a lot to grab the right syntax when I want to add a lifetime somewhere.
I came across:
without lifetime with lifetime
Vec<Route> Vec<Route<'a>>
&Route &'a Route
impl Clone for Route impl<'a> Clone for Route<'a>
|| ||:'a
There are so many different variations on the syntax. Sometimes you go from Vec<Route>
to <Vec<Route<'a>>
, means you have to introduce angle brackets. Sometimes you go just from &Route
to &'a Route
so you only add a 'a
to your existing code. A different time you have to add :'a
. I also don't get why we need to add <'a>
to the impl
keyword as well.
To be more clear, why not say lifetimes are always ^a
or something so that the above list would boil down to:
without lifetime with lifetime
Vec<Route> Vec<Route^a>
&Route &^a Route (or Route^a ?)
impl Clone for Route impl Clone for Route^a
|| ||^a