title: How to return a function from a function for the purpose of parameter injection?
body: <p>I am trying to return a function from another function.</p>
  <pre class="lang-rust prettyprint-override"><code>// Immediately run function passed in
  fn set_timeout(f: &amp;dyn Fn()) {
      f();
  }
  
  fn callback() -&gt; dyn Fn() {
      return fn() {
          println!(&quot;hi&quot;);
      };
  }
  
  fn main() {
    set_timeout(&amp;callback());
  }
  </code></pre>
  <p>My end goal is to achieve a long form variation of the following code where I move <code>fooRef1</code> into the inner function through a parameter on <code>callback(fooRef1)</code> (parameter injection).</p>
  <pre class="lang-rust prettyprint-override"><code>fn main() {
    let fooRef = Arc::new(Mutex::new(String::from(&quot;foo&quot;)));
    let fooRef1 = fooRef.clone();
  
    set_timeout(&amp;(move || {
      let mut foo = fooRef1.lock().unwrap();
      foo.push_str(&quot;bar&quot;);
      println!(&quot;{}&quot;, foo);
    }));
  }
  </code></pre>
  <p>Why does the inline callback closure work but the separated function does not?</p>
  <p>EDIT: Here's the error messages from rustc, as obtained via Rust playground.</p>
  <pre><code>   Compiling playground v0.0.1 (/playground)
  error: expected one of `.`, `;`, `?`, `}`, or an operator, found keyword `fn`
   --&gt; src/main.rs:7:12
    |
  7 |     return fn() {
    |            ^^ expected one of `.`, `;`, `?`, `}`, or an operator
  
  error[E0746]: return type cannot have an unboxed trait object
   --&gt; src/main.rs:6:18
    |
  6 | fn callback() -&gt; dyn Fn() {
    |                  ^^^^^^^^ doesn't have a size known at compile-time
    |
    = note: for information on trait objects, see &lt;https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types&gt;
    = note: if all the returned values were of the same type you could use `impl Fn()` as the return type
    = note: for information on `impl Trait`, see &lt;https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits&gt;
    = note: you can create a new `enum` with a variant for each returned type
  help: return a boxed trait object instead
    |
  6 | fn callback() -&gt; Box&lt;dyn Fn()&gt; {
  7 |     Box::new(return fn() {
  8 |         println!(&quot;hi&quot;);
  9 |     };)
    |
  
  error[E0277]: the size for values of type `(dyn Fn() + 'static)` cannot be known at compilation time
    --&gt; src/main.rs:13:16
     |
  13 |   set_timeout(&amp;callback());
     |                ^^^^^^^^ doesn't have a size known at compile-time
     |
     = help: the trait `Sized` is not implemented for `(dyn Fn() + 'static)`
     = note: the return type of a function must have a statically known size
  
  Some errors have detailed explanations: E0277, E0746.
  For more information about an error, try `rustc --explain E0277`.
  error: could not compile `playground` due to 3 previous errors
  </code></pre>