I recently tried to use impl Trait in the return position inside traits (stable branch) and encountered a behavior I'm curious about whether it's like this by design or if it is a bug. Here is an example I made to demonstrate it:
The above example works just fine, but if I change the factory function to the following (calling trait_fn):
fn factory() -> impl Display {
let ms = MyStruct;
ms.trait_fn()
}
It does not compile anymore, saying that the lifetime of ms is not static. My current thoughts are that because of the trait, it captures all the function context as it can't figure out what's inside the function. But then I have another question—is there a way to decouple the result from params? I played with manual lifetimes, but nothing worked for me so far.
The non-trait analogue to using an associated type or GAT will be TAIT. (Playground.)
(These unstable TAIT/GAT workarounds were intended to be the workaround, but probably aren't going to land soon enough and/or be ergonomic enough for the changes to RPIT capturing; hence RFC 3617.)
@quinedot Thanks a lot for the answer! I was hoping it's a work in progress that lends to the stable soon. 2024 doesn't sound too bad.
I am looking forward to it and I'd love to test it in the nightly once it is available.