How to `'a : 'b` if the trait does not know about lifetime `'b`?

How to specify lifetime restriction like 'a : 'b for implementation for a trait if the trait does not know about lifetime 'b introduced in implementation?

Here is the playground.

What is the magic here? How to get more practice to understand such nuances of lifetimes and type system better? What to read? I suppose Rustlings is not enough?

Is that solvable problem?

I don't think it is, but didn't reply before as I wasn't sure I'd exhausted all arcane possibilities.

In this particular case, the difficulty is referring to a lifetime that doesn't explicitly exist at the declaration site, arguably combined with 'lifetime: Type not being a valid bound.

As for how to learn these things better, I'm afraid I don't have a great answer. There is no consolidated guide on lifetimes. I've poked at writing one myself but it would probably take months. [1] There's a dozen or so RFCs you could read and some compiler documentation. [2] Even with those it's hard to put the pieces together into a comprehensive picture.

So I guess the best advice is to keep experimenting and accumulating knowledge. Reading enough answers to borrowing questions on this forum and then trying or succeeding to solve them myself has been a big source of practice for me.

  1. There are a few patterns in general I feel deserve a write-up, but trying to explain leads to a sort of "why is the sky blue" spiral. ↩︎

  2. Let me know if I should dump a bunch; it's probably hours of reading. ↩︎

1 Like