title: Type bound for a function with return lifetime
body: <p>Consider the function</p>
  <pre><code>fn f(v: &amp;[usize]) -&gt; impl Iterator&lt;Item = usize&gt; + '_ {
      v.iter().cloned()
  }
  </code></pre>
  <p>I want to write a generic function <code>g</code> which accepts any function with the same signature as <code>f</code>, and calls that function with various lifetimes. Is this possible?</p>
  <hr />
  <p><strong>My attempt 1</strong>: I naively wrote</p>
  <pre><code>fn g&lt;F&gt;(f: F)
  where
      F: for&lt;'a&gt; Fn(&amp;'a [usize]) -&gt; (impl Iterator&lt;Item = usize&gt; + 'a) {}
  </code></pre>
  <p>but I got</p>
  <pre><code>error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `Fn` trait return
  </code></pre>
  <hr />
  <p><strong>My attempt 2</strong>: I tried to give <code>g</code> another type parameter for the specific iterator type:</p>
  <pre><code>fn g&lt;F, I&gt;(f: F)
  where
      I: Iterator&lt;Item = usize&gt;,
      F: for&lt;'a&gt; Fn(&amp;'a [usize]) -&gt; I {}
  </code></pre>
  <p>I think this would work if the iterator were <code>'static</code>. But in this case I'd need <code>I</code> to be a higher kinded type with one lifetime parameter. Concretely, this <code>g</code> compiles but doesn't accept <code>f</code>.</p>
  <hr />
  <p><strong>My attempt 3</strong>: As above, but giving <code>g</code> a lifetime parameter to specialize <code>f</code>:</p>
  <pre><code>fn g&lt;'a, F, I&gt;(f: F)
  where
      I: Iterator&lt;Item = usize&gt; + 'a,
      F: Fn(&amp;'a [usize]) -&gt; I {}
  </code></pre>
  <p>This compiles and accepts <code>f</code>, but the body of <code>g</code> can only use <code>f</code> with the specific lifetime <code>'a</code>.</p>