title: How do I convert a Vec<T> to a Vec<U> without copying the vector?
body: <p>I want to convert a <code>Vec&lt;T&gt;</code> to a <code>Vec&lt;U&gt;</code> where <code>T</code> is a primitive of some sort and <code>U</code> is a newtype of <code>T</code>: <code>struct U(T)</code>. </p>
  
  <p>I tried something like this:</p>
  
  <pre><code>struct Foo(u32);
  
  fn do_something_using_foo(buffer: &amp;mut Vec&lt;Foo&gt;) {}
  
  fn main() {
      let buffer: Vec&lt;u32&gt; = vec![0; 100];
  
      do_something_using_foo(&amp;mut buffer as Vec&lt;Foo&gt;);
  }
  </code></pre>
  
  <p>I don't want to make a copy of the vector, I want to wrap the <code>u32</code> fields in the newtype <code>Foo</code>.</p>
  
  <p>This gives the error:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0308]: mismatched types
   --&gt; main.rs:8:28
    |
  8 |     do_something_using_foo(&amp;mut buffer as Vec&lt;Foo&gt;);
    |                            ^^^^^^^^^^^^^^^^^^^^^^^ expected mutable reference, found struct `std::vec::Vec`
    |
    = note: expected type `&amp;mut std::vec::Vec&lt;Foo&gt;`
           found type `std::vec::Vec&lt;Foo&gt;`
    = help: try with `&amp;mut &amp;mut buffer as Vec&lt;Foo&gt;`
  
  error: non-scalar cast: `&amp;mut std::vec::Vec&lt;u32&gt;` as `std::vec::Vec&lt;Foo&gt;`
   --&gt; main.rs:8:28
    |
  8 |     do_something_using_foo(&amp;mut buffer as Vec&lt;Foo&gt;);
    |                            ^^^^^^^^^^^^^^^^^^^^^^^
  
  error: aborting due to previous error(s)
  </code></pre>