title: How do I write a function that takes both owned and non-owned string collections?
body: <p>I'm having trouble writing a function that takes a collection of strings as parameter. My function looks like this:</p>
  
  <pre><code>type StrList&lt;'a&gt; = Vec&lt;&amp;'a str&gt;;
  
  fn my_func(list: &amp;StrList) {
      for s in list {
          println!("{}", s);
      }
  }
  </code></pre>
  
  <p>All goes well if I pass a <code>Vec&lt;&amp;'a str&gt;</code> to the function, as expected. However, if I pass a <code>Vec&lt;String&gt;</code> the compiler complains:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0308]: mismatched types
    --&gt; src/main.rs:13:13
     |
  13 |     my_func(&amp;v2);
     |             ^^^ expected &amp;str, found struct `std::string::String`
     |
     = note: expected type `&amp;std::vec::Vec&lt;&amp;str&gt;`
     = note:    found type `&amp;std::vec::Vec&lt;std::string::String&gt;`
  </code></pre>
  
  <p>This is the main used:</p>
  
  <pre><code>fn main() {
      let v1 = vec!["a", "b"];
      let v2 = vec!["a".to_owned(), "b".to_owned()];
      my_func(&amp;v1);
      my_func(&amp;v2);
  }
  </code></pre>
  
  <p>My function is not able to take vectors of owned strings. Conversely, if I change the <code>StrList</code> type into:</p>
  
  <pre><code>type StrList = Vec&lt;String&gt;;
  </code></pre>
  
  <p>The first call fails, and the second works.</p>
  
  <p>A possible solution is to produce a <code>Vec&lt;&amp;'a str&gt;</code> from <code>v2</code> in this way:</p>
  
  <pre><code>let v2_1 : Vec&lt;_&gt; = v2.iter().map(|s| s.as_ref()).collect();
  </code></pre>
  
  <p>But it seems very odd to me. <code>my_func</code> should not care about the ownership of the strings.</p>
  
  <p>What kind of signature should I use for <code>my_func</code> to support both vectors of owned strings and string references?</p>