title: Trying to copy content of a Vec into an other, how to use copy_from_slice()?
body: <p>I'm  trying to <strong>copy</strong> the content of a <code>Vec</code> into an existing <code>Vec</code>, replacing the content of the target <code>Vec</code>.</p>
  
  <p>Here is what I'm looking for:</p>
  
  <ul>
  <li>allocation of new memory shouldn't be needed (unless the target <code>Vec</code> is too short) because the target <code>Vec</code> is already allocated</li>
  <li>it should not use an iterator because a memcopy should be good enough do the job</li>
  <li>the source <code>Vec</code> shouldn't be changed</li>
  <li>ideally it should use safe methods</li>
  </ul>
  
  <p>Here is what I tried:</p>
  
  <ul>
  <li><code>vec.clone()</code> : gives correct content but allocates new memory, which should not be needed for a copy into an already existing big enough <code>Vec</code></li>
  <li><code>vec.clear(); vec.extend();</code> : copies in place, but seems to use an iterator on each element, which should not be needed, I just want a memcopy</li>
  <li><code>vec.copy_from_slice()</code> : is what I'm looking for, but needs exactly the same size buffers, which for some reason I can't seem to get</li>
  </ul>
  
  <h2>What doesn't work</h2>
  
  <pre><code>#![feature(shrink_to)]
  fn vec_copy(src: &amp;Vec&lt;i32&gt;, dst: &amp;mut Vec&lt;i32&gt;) {
      // Try to adjust dst buffer size... there should be a better way
      if src.len() &gt; dst.len() {
          let addon = src.len() - dst.len();
          dst.reserve_exact(addon);
      } else {
          dst.shrink_to(src.len());
      }
      // Do the copy        
      // panics! :
      // thread 'main' panicked at libcore/slice/mod.rs:1645:9
      // 'destination and source slices have different lengths'
      // 
      dst.copy_from_slice(src.as_slice()); // &lt;--- panics here
  }
  
  fn main() {
      // Copy from a shorter Vec
      let mut container = vec![1, 2];
      let test1 = vec![3]; // shorter vec
      println!("{:p} = {:?}", &amp;container[0], container); // output: 0x7f00bda20008 = [1, 2]
      vec_copy(&amp;test1, &amp;mut container); // panics inside function 
      println!("{:p} = {:?}", &amp;container[0], container); // expected: 0x7f00bda20008 = [3]
  
      // Copy from a longer Vec
      container = vec![1, 2];    
      let test2 = vec![4, 5, 6]; // longer Vec
      println!("{:p} = {:?}", &amp;container[0], container); // output: 0x7fef5b820018 = [1, 2]
      vec_copy(&amp;test2, &amp;mut container); // panics inside function
      println!("{:p} = {:?}", &amp;container[0], container); // expected: 0x7fef5b820018 = [4, 5, 6]    
  }
  </code></pre>
  
  <p><strong>Panics with error:</strong> </p>
  
  <pre class="lang-none prettyprint-override"><code>thread 'main' panicked at libcore/slice/mod.rs:1645:9,
  'destination and source slices have different lengths'
  </code></pre>
  
  <h2>The question</h2>
  
  <p>Using <code>vec.copy_from_slice()</code> seems to be the way to memcopy in place the content of a <code>Vec</code> into an other, without unneeded memory allocation and without using an iterator.</p>
  
  <p>How can I set the size of the target <code>Vec</code> so that <code>vec.copy_from_slice()</code> doesn't panic?</p>