title: How to implement IntoIterator on a StackVec
body: <p>I am trying to write a <code>Vec</code> like object with a fixed size to be used on the stack.</p>
  
  <p>This is part of an online course I am trying to complete <a href="https://cs140e.sergio.bz/assignments/1-shell/" rel="nofollow noreferrer">Assignment 1, Phase 2A</a>.</p>
  
  <p>I am having problems implementing the <code>IntoIterator</code> trait.</p>
  
  <p>The code for the <code>StructVec</code></p>
  
  <pre class="lang-rust prettyprint-override"><code>#![no_std]
  
  pub struct StackVec&lt;'a, T: 'a&gt; {
      storage: &amp;'a mut [T],
      len: usize
  }
  
  impl&lt;'a, T: 'a&gt; StackVec&lt;'a, T&gt; {
      pub fn new(storage: &amp;'a mut [T]) -&gt; StackVec&lt;'a, T&gt; {
          StackVec { storage, len: 0 }
      }
  
      pub fn with_len(storage: &amp;'a mut [T], len: usize) -&gt; StackVec&lt;'a, T&gt; {
          StackVec { storage, len }
      }
  
      pub fn capacity(&amp;self) -&gt; usize {
          self.storage.len()
      }
  
      pub fn truncate(&amp;mut self, len: usize) {
          if len &lt; self.len {
              self.len = len;
          }
      }
  
      pub fn into_slice(self) -&gt; &amp;'a mut [T] {
          &amp;mut self.storage[..self.len]
      }
  
      pub fn as_slice(&amp;self) -&gt; &amp;[T] {
          &amp;self.storage[..self.len]
      }
  
      pub fn as_mut_slice(&amp;mut self) -&gt; &amp;mut [T] {
          &amp;mut self.storage[..self.len]
      }
  
      pub fn len(&amp;self) -&gt; usize {
          self.len
      }
  
      pub fn is_empty(&amp;self) -&gt; bool {
          self.len == 0
      }
  
      pub fn is_full(&amp;self) -&gt; bool {
          self.len == self.storage.len()
      }
  
      pub fn push(&amp;mut self, value: T) -&gt; Result&lt;(), ()&gt; {
          if self.is_full() {
              return Err(());
          }
          self.storage[self.len] = value;
          self.len += 1;
          Ok(())
      }
  }
  </code></pre>
  
  <p>I was able to get implement the <code>IntoIterator</code> trait by returning the iterator of the underlying array:</p>
  
  <pre class="lang-rust prettyprint-override"><code>impl&lt;'a, T: 'a&gt; IntoIterator for StackVec&lt;'a, T&gt; {
      type Item = &amp;'a mut T;
      type IntoIter = core::slice::IterMut&lt;'a, T&gt;;
  
      fn into_iter(self) -&gt; Self::IntoIter {
          self.storage.into_iter()
      }
  }
  </code></pre>
  
  <p>However that's not really what I want as it will iterate through the entire array instead of just the items that were pushed onto it.</p>
  
  <p>I've tried returning an iterator from an array slice of the underlying array but I get caught out by lifetimes:</p>
  
  <pre class="lang-rust prettyprint-override"><code>impl&lt;'a, T: 'a&gt; IntoIterator for StackVec&lt;'a, T&gt; {
      type Item = &amp;'a T;
      type IntoIter = core::slice::Iter&lt;'a, T&gt;;
  
      fn into_iter(self) -&gt; Self::IntoIter {
          self.as_slice().into_iter()
      }
  }
  </code></pre>
  
  <p>Fails to compile with the error</p>
  
  <pre><code>error[E0597]: `self` does not live long enough
     --&gt; src/lib.rs:165:9
      |
  165 |         self.as_slice().into_iter()
      |         ^^^^ borrowed value does not live long enough
  166 |     }
      |     - borrowed value only lives until here
      |
  note: borrowed value must be valid for the lifetime 'a as defined on the impl at 160:1...
     --&gt; src/lib.rs:160:1
      |
  160 | / impl&lt;'a, T: 'a&gt; IntoIterator for StackVec&lt;'a, T&gt; {
  161 | |     type Item = &amp;'a T;
  162 | |     type IntoIter = core::slice::Iter&lt;'a, T&gt;;
  163 | |
  ...   |
  166 | |     }
  167 | | }
      | |_^
  </code></pre>
  
  <p>I've also tried creating a new <code>Iterator</code> entirely but I get caught out again!</p>
  
  <pre class="lang-rust prettyprint-override"><code>struct Iter&lt;'a, T: 'a&gt; {
      stack_vec: StackVec&lt;'a, T&gt;,
      start: usize,
  }
  
  impl&lt;'a, T: 'a&gt; Iterator for Iter&lt;'a, T&gt; {
      type Item = T;
  
      fn next(&amp;mut self) -&gt; Option&lt;Self::Item&gt; {
          let start = self.start;
          if start &lt; self.stack_vec.len {
              self.start += 1;
              Some(self.stack_vec[start])
          } else {
              None
          }
      }
  
      fn size_hint(&amp;self) -&gt; (usize, Option&lt;usize&gt;) {
          let size = self.stack_vec.len - self.start;
          (size, Some(size))
      }
  }
  </code></pre>
  
  <p>Fails to compile with the error:</p>
  
  <pre><code>error[E0508]: cannot move out of type `[T]`, a non-copy slice
     --&gt; src/lib.rs:148:18
      |
  148 |             Some(self.stack_vec[start])
      |                  ^^^^^^^^^^^^^^^^^^^^^ cannot move out of here
  </code></pre>
  
  <p>I've tried looking at crates that do the same thing but either they use the <code>std</code> library which I can't use for this assignment, or I don't understand how to port their code to mine.</p>
  
  <p><a href="https://github.com/bluss/arrayvec" rel="nofollow noreferrer">https://github.com/bluss/arrayvec</a></p>
  
  <p><a href="https://github.com/danielhenrymantilla/stackvec-rs/blob/master/src/stackvec/traits/into_iter.rs" rel="nofollow noreferrer">https://github.com/danielhenrymantilla/stackvec-rs/blob/master/src/stackvec/traits/into_iter.rs</a></p>
  
  <p>What am I doing wrong?</p>