title: How do I create an array of unboxed functions / closures?
body: <blockquote>
    <p>Editor's note: This question was asked before Rust 1.0 and some of the syntax has changed since then, but the underlying concepts remain. Some answers have been updated for Rust 1.0 syntax.</p>
  </blockquote>
  
  <p>I'm new to Rust and trying to do something with closures which is trivial in JavaScript, Python, etc. but I am running into lifetime issues in Rust.  I understand the error message, but it leads me to believe what I want to do is pretty hard in Rust.</p>
  
  <p>I just want to create an array of functions, <code>a</code>, such that</p>
  
  <ul>
  <li><code>a[0]</code> is the function returning 0</li>
  <li><code>a[1]</code> is the function returning 1</li>
  <li>...</li>
  <li><code>a[9]</code> is the function returning 9</li>
  </ul>
  
  <p>I tried:</p>
  
  <pre><code>fn main() {
      let a : [||-&gt;uint,  ..10];
      for i in range(0u, 10) {
          a[i] = ||{i};
      }
      println!("{} {} {}", a[1](), a[5](), a[9]())
  }
  </code></pre>
  
  <p>But I got a lifetime error. The reported error was "cannot infer an appropriate lifetime due to conflicting requirements" for the functions in <code>a</code> because the lifetimes cannot outlive the <code>while</code> block so that the closures cannot outlive their stack frame, which of course they would because I am invoking them in <code>println!</code>.</p>
  
  <p>I'm sure there must be a way to build up this array of functions, but how?</p>