title: How can I create a stack-allocated vector-like container?
body: <p>How would you go about creating a stack-allocated vector-like container with some fixed upper limit on the number of elements it can contain? You can see my attempt at this below, but it doesn't compile:</p>
  
  <pre><code>// The following is at crate level
  #![feature(unsafe_destructor)]
  
  use std::mem;
  use std::ptr;
  use std::slice::Iter;
  
  pub struct StackVec&lt;T&gt; {
      buf: [T; 10],
      len: usize,
  }
  
  impl&lt;T&gt; StackVec&lt;T&gt; {
      pub fn new() -&gt; StackVec&lt;T&gt; {
          StackVec {
              buf: unsafe { mem::uninitialized() },
              len: 0,
          }
      }
  
      pub fn iter(&amp;self) -&gt; Iter&lt;T&gt; {
          (&amp;self.buf[..self.len]).iter()
      }
  
      pub fn push(&amp;mut self, value: T) {
          unsafe { ptr::write(self.buf.get_mut(self.len).unwrap(), value); }
          self.len += 1;
      }
  
      pub fn pop(&amp;mut self) -&gt; Option&lt;T&gt; {
          if self.len == 0 {
              None
          } else {
              unsafe {
                  self.len -= 1;
                  Some(ptr::read(self.buf.get(self.len).unwrap()))
              }
          }
      }
  }
  
  #[unsafe_destructor]
  impl&lt;T&gt; Drop for StackVec&lt;T&gt;
      where T: Drop
  {
      fn drop(&amp;mut self) {
          for elem in self.iter() {
              unsafe { ptr::read(elem); }
          }
          unsafe { mem::forget(self.buf); } // ERROR: [1]
      }
  }
  </code></pre>
  
  <p>This is the compile-time error I get:<br>
  [1] error: cannot move out of type <code>stackvec::StackVec&lt;T&gt;</code>, which defines the <code>Drop</code> trait</p>