title: How can I store a closure object in a struct?
body: <p>I can't figure out how to store a closure object in a struct. The arguments and return for the closure object are known. Here's my condensed code:</p>
  <pre><code>struct Instr&lt;F&gt;
      where F: Fn([i32;4],[i32;3]) -&gt; [i32;4]
  {
      name: String,
      op: F
  }
  
  fn main()
  {
      // Simple example showing the difficulty:
      let tmp : Instr&lt;Fn([i32;4],[i32;3]) -&gt; [i32;4]&gt; = Instr { name: &quot;asd&quot;.to_string(), op: |a,b| a};
  
      // What I really want is something more like this:
      // let instrs = vec![
      //     Instr { name: &quot;asdf&quot;, op: |a,b| a },
      //     Instr { name: &quot;qwer&quot;, op: |a,b| a }
      // ];
  }
  </code></pre>
  <p>Frankly I don't understand what any of these errors mean.  In my mind this is simple.  The closure has a type, and a known size.  It should be straightforward to store it in a typed field of the same type.  Right?</p>
  <p>Attempting to add <code>F: ?Sized</code> as the error messages suggest does not fix the &quot;size not known at compile time&quot; errors.</p>
  <p>Could someone help me get this compiling correctly?</p>
  <pre class="lang-none prettyprint-override"><code>error[E0277]: the size for values of type `dyn Fn([i32; 4], [i32; 3]) -&gt; [i32; 4]` cannot be known at compilation time
    --&gt; a.rs:11:15
     |
  1  | struct Instr&lt;F&gt;
     |              - required by this bound in `Instr`
  ...
  11 |     let tmp : Instr&lt;Fn([i32;4],[i32;3]) -&gt; [i32;4]&gt; = Instr { name: &quot;asd&quot;.to_string(), op: |a,b| a};
     |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
     |
     = help: the trait `Sized` is not implemented for `dyn Fn([i32; 4], [i32; 3]) -&gt; [i32; 4]`
  help: you could relax the implicit `Sized` bound on `F` if it were used through indirection like `&amp;F` or `Box&lt;F&gt;`
    --&gt; a.rs:1:14
     |
  1  | struct Instr&lt;F&gt;
     |              ^ this could be changed to `F: ?Sized`...
  2  |     where F : Fn([i32;4],[i32;3]) -&gt; [i32;4]
     |           - ...if indirection was used here: `Box&lt;F&gt;`
  ...
  5  |     op : F
     |          - ...if indirection was used here: `Box&lt;F&gt;`
  
  error[E0277]: the size for values of type `dyn Fn([i32; 4], [i32; 3]) -&gt; [i32; 4]` cannot be known at compilation time
    --&gt; a.rs:11:55
     |
  1  | / struct Instr&lt;F&gt;
  2  | |     where F : Fn([i32;4],[i32;3]) -&gt; [i32;4]
  3  | | {
  4  | |     name : String,
  5  | |     op : F
  6  | | }
     | |_- required by `Instr`
  ...
  11 |       let tmp : Instr&lt;Fn([i32;4],[i32;3]) -&gt; [i32;4]&gt; = Instr { name: &quot;asd&quot;.to_string(), op: |a,b| a};
     |                                                         ^^^^^ doesn't have a size known at compile-time
     |
     = help: the trait `Sized` is not implemented for `dyn Fn([i32; 4], [i32; 3]) -&gt; [i32; 4]`
  
  error[E0308]: mismatched types
    --&gt; a.rs:11:92
     |
  11 |     let tmp : Instr&lt;Fn([i32;4],[i32;3]) -&gt; [i32;4]&gt; = Instr { name: &quot;asd&quot;.to_string(), op: |a,b| a};
     |                                                                                            ^^^^^^^ expected trait object `dyn Fn`, found closure
     |
     = note: expected trait object `dyn Fn([i32; 4], [i32; 3]) -&gt; [i32; 4]`
                     found closure `[closure@a.rs:11:92: 11:99]`
  </code></pre>