These are chat archives for rust-lang/rust

Jan 2018
Jan 20 2018 10:34 UTC

Hi guys! Can you help me out with that code:

trait SomeTrait {}

fn foo<P, Q, F>(func: F)
        Q: SomeTrait,
        F: for<'a> Fn(&'a P) -> Q + 'a


I need to tell compiler that F returns smth that implements SomeTrait with lifetime 'a
But I get this error:

error[E0261]: use of undeclared lifetime name `'a`
  --> src/
21 |         F: for<'a> Fn(&'a P) -> Q + 'a
   |                                     ^^ undeclared lifetime

error: aborting due to previous error

error: Could not compile `playground`.
Michal 'vorner' Vaner
Jan 20 2018 10:39 UTC
I think you can just omit the bound. But otherwise, I think you might be writing something like:
fn foo<'a, P, Q, F>(func: F)
    Q: SomeTrait + 'a,
    F: Fn(&'a P) -> Q
Denis Lisov
Jan 20 2018 10:39 UTC
@alleycat-at-git Like this?
Michal 'vorner' Vaner
Jan 20 2018 10:39 UTC
Hmm. Thinking about that, it probably isn't what you want
Jan 20 2018 10:43 UTC
Yeah, that should work, but I wonder is there a way to make it work with for syntax. Like in one line
Like for<'a> Fn(&'a P) -> (Q + 'a)
Jan 20 2018 15:33 UTC
Actually that solution didn't work for me, it turns out that when you do fn foo<'a>, then anything marked with this lifetime must live at least as long as function body, which is not the case for me
So I need to figure out how to do it with for syntax
Jan 20 2018 16:56 UTC
Where is the for<'a>... syntax in trait bounds documented? I couldn't find it with an online search and the only place I've run into it is here.