title: Implementing trait for Fn(&something) in Rust
body: <p>Here is a working sample to implement <code>Runnable</code> for <code>Fn()</code> (so that we can directly pass a &amp;closure to the <code>run_the_runnable</code> function):</p>
  
  <pre><code>trait Runnable {
      fn run(&amp;self);
  }
  
  impl&lt;F&gt; Runnable for F where F: Fn() {
      fn run(&amp;self) {
          self();
      }
  }
  
  fn run_the_runnable(runnable: &amp;Runnable) {
      runnable.run();
  }
  
  fn main() {
      // runnable without parameters
      struct MyRunnable;
      impl Runnable for MyRunnable {
          fn run(&amp;self) {
              println!("Hello from MyRunnable");
          }
      }
  
      // from struct instance (WORKS)
      run_the_runnable(&amp;MyRunnable);
  
      // from closure (WORKS)
      run_the_runnable(&amp;|| {
          println!("Hello from run() closure");
      });
  }
  </code></pre>
  
  <p>(<a href="https://play.rust-lang.org/?code=trait%20Runnable%20%7B%0A%20%20%20%20fn%20run%28%26self%29%3B%0A%7D%0A%0Aimpl%3CF%3E%20Runnable%20for%20F%20where%20F%3A%20Fn%28%29%20%7B%0A%20%20%20%20fn%20run%28%26self%29%20%7B%0A%20%20%20%20%20%20%20%20self%28%29%3B%0A%20%20%20%20%7D%0A%7D%0A%0Afn%20run_the_runnable%28runnable%3A%20%26Runnable%29%20%7B%0A%20%20%20%20runnable.run%28%29%3B%0A%7D%0A%0Afn%20main%28%29%20%7B%0A%20%20%20%20%2F%2F%20runnable%20without%20parameters%0A%20%20%20%20struct%20MyRunnable%3B%0A%20%20%20%20impl%20Runnable%20for%20MyRunnable%20%7B%0A%20%20%20%20%20%20%20%20fn%20run%28%26self%29%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20println!%28%22Hello%20from%20MyRunnable%22%29%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%0A%20%20%20%20%2F%2F%20from%20struct%20instance%20%28WORKS%29%0A%20%20%20%20run_the_runnable%28%26MyRunnable%29%3B%0A%0A%20%20%20%20%2F%2F%20from%20closure%20%28WORKS%29%0A%20%20%20%20run_the_runnable%28%26%7C%7C%20%7B%0A%20%20%20%20%20%20%20%20println!%28%22Hello%20from%20run%28%29%20closure%22%29%3B%0A%20%20%20%20%7D%29%3B%0A%7D&amp;version=stable&amp;backtrace=0" rel="nofollow noreferrer">Rust playground</a>)</p>
  
  <p>Now, let's change the runnable <code>run()</code> method to accept a reference parameter (<code>&amp;i32</code>), and implement <code>Fn(&amp;i32)</code>:</p>
  
  <pre><code>trait Runnable {
      fn run(&amp;self, x: &amp;i32);
  }
  
  impl&lt;F&gt; Runnable for F where F: Fn(&amp;i32) {
      fn run(&amp;self, x: &amp;i32) {
          self(x);
      }
  }
  
  fn run_the_runnable(runnable: &amp;Runnable, x: &amp;i32) {
      runnable.run(x);
  }
  
  fn main() {
      // runnable without parameters
      struct MyRunnable;
      impl Runnable for MyRunnable {
          fn run(&amp;self, x: &amp;i32) {
              println!("Hello from MyRunnable {}", x);
          }
      }
  
      let x = 42;
  
      // from struct instance (WORKS)
      run_the_runnable(&amp;MyRunnable, &amp;x);
  
      // from closure (DOES NOT WORK)
      run_the_runnable(&amp;|x| {
          println!("Hello from run(&amp;i32) closure {}", x);
      }, &amp;x);
  }
  </code></pre>
  
  <p>(<a href="https://play.rust-lang.org/?code=trait%20Runnable%20%7B%0A%20%20%20%20fn%20run%28%26self%2C%20x%3A%20%26i32%29%3B%0A%7D%0A%0Aimpl%3CF%3E%20Runnable%20for%20F%20where%20F%3A%20Fn%28%26i32%29%20%7B%0A%20%20%20%20fn%20run%28%26self%2C%20x%3A%20%26i32%29%20%7B%0A%20%20%20%20%20%20%20%20self%28x%29%3B%0A%20%20%20%20%7D%0A%7D%0A%0Afn%20run_the_runnable%28runnable%3A%20%26Runnable%2C%20x%3A%20%26i32%29%20%7B%0A%20%20%20%20runnable.run%28x%29%3B%0A%7D%0A%0Afn%20main%28%29%20%7B%0A%20%20%20%20%2F%2F%20runnable%20without%20parameters%0A%20%20%20%20struct%20MyRunnable%3B%0A%20%20%20%20impl%20Runnable%20for%20MyRunnable%20%7B%0A%20%20%20%20%20%20%20%20fn%20run%28%26self%2C%20x%3A%20%26i32%29%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20println!%28%22Hello%20from%20MyRunnable%20%7B%7D%22%2C%20x%29%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%0A%20%20%20%20let%20x%20%3D%2042%3B%0A%0A%20%20%20%20%2F%2F%20from%20struct%20instance%20%28WORKS%29%0A%20%20%20%20run_the_runnable%28%26MyRunnable%2C%20%26x%29%3B%0A%0A%20%20%20%20%2F%2F%20from%20closure%20%28DOES%20NOT%20WORK%29%0A%20%20%20%20run_the_runnable%28%26%7Cx%7C%20%7B%0A%20%20%20%20%20%20%20%20println!%28%22Hello%20from%20run%28%26i32%29%20closure%20%7B%7D%22%2C%20x%29%3B%0A%20%20%20%20%7D%2C%20%26x%29%3B%0A%7D&amp;version=stable&amp;backtrace=0" rel="nofollow noreferrer">Rust playground</a>)</p>
  
  <p>Passing the closure does not work anymore:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0271]: type mismatch resolving `for&lt;'r&gt; &lt;[closure@&lt;anon&gt;:30:27: 32:10] as std::ops::FnOnce&lt;(&amp;'r i32,)&gt;&gt;::Output == ()`
    --&gt; &lt;anon&gt;:30:26
     |
  30 |           run_the_runnable(&amp;|x| {
     |  __________________________^ starting here...
  31 | |             println!("Hello from run(&amp;i32) closure {}", x);
  32 | |         }, &amp;x);
     | |_________^ ...ending here: expected bound lifetime parameter , found concrete lifetime
     |
     = note: concrete lifetime that was found is lifetime '_#63r
     = note: required because of the requirements on the impl of `Runnable` for `[closure@&lt;anon&gt;:30:27: 32:10]`
     = note: required for the cast to the object type `Runnable`
  
  error[E0281]: type mismatch: the type `[closure@&lt;anon&gt;:30:27: 32:10]` implements the trait `std::ops::Fn&lt;(_,)&gt;`, but the trait `for&lt;'r&gt; std::ops::Fn&lt;(&amp;'r i32,)&gt;` is required (expected concrete lifetime, found bound lifetime parameter )
    --&gt; &lt;anon&gt;:30:26
     |
  30 |           run_the_runnable(&amp;|x| {
     |  __________________________^ starting here...
  31 | |             println!("Hello from run(&amp;i32) closure {}", x);
  32 | |         }, &amp;x);
     | |_________^ ...ending here
     |
     = note: required because of the requirements on the impl of `Runnable` for `[closure@&lt;anon&gt;:30:27: 32:10]`
     = note: required for the cast to the object type `Runnable`
  </code></pre>
  
  <p>There are many questions about this error:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/41087239/expected-concrete-lifetime-found-bound-lifetime-parameter-when-storing-a-fn-in">Expected concrete lifetime, found bound lifetime parameter when storing a fn in a struct</a></li>
  <li><a href="https://stackoverflow.com/questions/32194367/expected-bound-lifetime-parameter-found-concrete-lifetime">Expected bound lifetime parameter, found concrete lifetime</a></li>
  <li><a href="https://stackoverflow.com/questions/24847331/rust-lifetime-error-expected-concrete-lifetime-but-found-bound-lifetime">Rust lifetime error expected concrete lifetime but found bound lifetime</a></li>
  <li>…</li>
  </ul>
  
  <p>But I am still not able to resolve this specific lifetime problem.</p>