Lifetime of a lifetime?

I was reading through https://dev.to/dandyvica/yarit-yet-another-rust-iterators-tutorial-46dk and saw the following:

  type Item = &'a &'a str;

What does this actually do/say? It looks like it's constraining lifetimes with two levels of indirection, but I have no idea what that would mean.

It's not lifetime of a lifetime, it's a pointer (with lifetime) to a pointer (with a lifetime) to str;

let x: String = "abc".to_string();
let y = &&x;

2 Likes

Well that's pretty obvious, now that you say it. Every time I read code with a lifetime syntax I haven't seen before I assume it's something entirely new and very complicated.

I should probably try to get out of that mindset. :slight_smile:

Thanks!

The lifetime is just an annotation. It says that both references share the same lifetime.

In the case of &'a &'b str neither reference shares the same lifetime (though this example does not specify how they overlap, it’s implied that 'b: 'a).

2 Likes

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