I’ve just started off with Rust a month ago, and I’m finding it a fascinating language, but I’ve got a bit stuck on this:

I want to make a trait similar to the below structure (my particular use case is a bit more complex but this captures the issue and error I’m getting). The issue I have is which lifetimes in the last impl. Note all the impls themselves will compile as independent functions which I believe means with enough genericity I should be able to place them under one trait. How can I sort out the lifetimes (for both the trait and the impls) so this compiles?

```
trait MyTrait<TIn> {
fn g<TOut, F>(f: F, x: Self) -> TOut
where
F: Fn(TIn) -> TOut;
}
impl<T> MyTrait<T> for T {
fn g<TOut, F>(f: F, x: T) -> TOut
where
F: Fn(T) -> TOut,
{
f(x)
}
}
impl<T> MyTrait<T> for &T
where
T: Clone,
{
fn g<TOut, F>(f: F, x: &T) -> TOut
where
F: Fn(T) -> TOut,
{
f(x.clone())
}
}
// This impl fails to compile:
impl<T> MyTrait<&T> for T {
fn g<TOut, F>(f: F, x: T) -> TOut
where
F: Fn(&T) -> TOut,
{
f(&x)
}
}
```

(Note, this is a cross post from StackOverflow)

(Edit: renamed function names from `f`

to `g`

so they don’t clash with parameters and cause confusion)