title: How can I create an iterator of &T from either a &Vec<T> or Vec<&T>?
body: <p>I have an enum with two variants. Either it contains a reference to a <code>Vec</code> of <code>String</code>s or it contains a <code>Vec</code> of references to <code>String</code>s:</p>
  
  <pre class="lang-rust prettyprint-override"><code>enum Foo&lt;'a&gt; {
      Owned(&amp;'a Vec&lt;String&gt;),
      Refs(Vec&lt;&amp;'a String&gt;),
  }
  </code></pre>
  
  <p>I want to iterate over references to the <code>String</code>s in this enum.</p>
  
  <p>I tried to implement a method on <code>Foo</code>, but don't know how to make it return the right iterator:</p>
  
  <pre class="lang-rust prettyprint-override"><code>impl&lt;'a&gt; Foo&lt;'a&gt; {
      fn get_items(&amp;self) -&gt; Iter&lt;'a, String&gt; {
          match self {
              Foo::Owned(v) =&gt; v.into_iter(),
              Foo::Refs(v) =&gt; /* what to put here? */,
          }
      }
  }
  
  fn main() {
      let test: Vec&lt;String&gt; = vec!["a".to_owned(), "b".to_owned()];
      let foo = Foo::Owned(&amp;test);
  
      for item in foo.get_items() {
          // item should be of type &amp;String here
          println!("{:?}", item);
      }
  }
  </code></pre>
  
  <p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=87aa534962775da8816495734901607b" rel="nofollow noreferrer">playground</a></p>
  
  <p>What is an idiomatic method to achieve this abstraction over <code>&amp;Vec&lt;T&gt;</code> and <code>Vec&lt;&amp;T&gt;</code>? <code>get_items</code> may also return something different, as long as it implements the <code>IntoIterator</code> trait so that I can use it in the <code>for</code> loop.</p>