1 The `impl Trait` return type captures lifetime parameters that do not
2 appear within the `impl Trait` itself.
4 Erroneous code example:
11 impl<'a, 'b> Trait<'b> for Cell<&'a u32> { }
13 fn foo<'x, 'y>(x: Cell<&'x u32>) -> impl Trait<'y>
20 Here, the function `foo` returns a value of type `Cell<&'x u32>`,
21 which references the lifetime `'x`. However, the return type is
22 declared as `impl Trait<'y>` -- this indicates that `foo` returns
23 "some type that implements `Trait<'y>`", but it also indicates that
24 the return type **only captures data referencing the lifetime `'y`**.
25 In this case, though, we are referencing data with lifetime `'x`, so
26 this function is in error.
28 To fix this, you must reference the lifetime `'x` from the return
29 type. For example, changing the return type to `impl Trait<'y> + 'x`
37 impl<'a,'b> Trait<'b> for Cell<&'a u32> { }
39 fn foo<'x, 'y>(x: Cell<&'x u32>) -> impl Trait<'y> + 'x