title: How to define lifetimes on a Fn trait bound returning references?
body: <p>I'm trying to create a struct with a field, generic over <code>F</code> where <code>F</code> implements something like: <code>Fn(&amp;mut Compiler, &amp;[Token]) -&gt; &amp;Token</code>. The only issue is, I'm not sure how I define lifetimes on the <code>Fn</code> trait which satisfy the constraint that the returned <code>&amp;Token</code> references data in the <code>&amp;[Token]</code> slice supplied as an argument. Everything I've tried has thrown cryptic errors thus far. </p>
  
  <p>Here is an MVCE which demonstrates the code (without any lifetimes):</p>
  
  <pre><code>struct Compiler;
  #[derive(Debug)]
  struct Token(usize);
  
  impl Compiler {
      // missing lifetime paramters here
      fn meth(&amp;mut self, tokens: &amp;[Token]) -&gt; &amp;Token {
          tokens.get(0).unwrap()
      }
  }
  
  // missing lifetime paramters here    
  struct Rule&lt;F&gt; where F: Fn(&amp;mut Compiler, &amp;[Token]) -&gt; &amp;Token {
      func: F
  }
  
  fn main() {
      let mut c = Compiler;
      let tokens = vec![Token(0), Token(1), Token(2)];
      let r = Rule { func: Compiler::meth };
      (r.func)(&amp;mut c, &amp;tokens);
  }
  </code></pre>
  
  <p>Naturally this fails to compile with the error:</p>
  
  <pre><code>   Compiling playground v0.0.1 (/playground)
  error[E0106]: missing lifetime specifier
    --&gt; src/main.rs:11:56
     |
  11 | struct Rule&lt;F&gt; where F: Fn(&amp;mut Compiler, &amp;[Token]) -&gt; &amp;Token {
     |                                                        ^ expected lifetime parameter
     |
     = help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from argument 1 or argument 2
  </code></pre>
  
  <p>I've attempted to add lifetimes specifiers here and there, moving things around but nothing seems to work. I would really appreciate any insight into the issue. Thanks!</p>