title: Convert vectors to arrays and back
body: <p>I am attempting to figure the most Rust-like way of converting from a vector to array and back. These macros will work and can even be made generic with some unsafe blocks, but it all feels very un-Rust like.</p>
  
  <p>I would appreciate any input and hold no punches, I think this code is far from nice or optimal. I have only played with Rust for a few weeks now and chasing releases and docs so really appreciate help.</p>
  
  <pre><code>macro_rules! convert_u8vec_to_array {
      ($container:ident, $size:expr) =&gt; {{
      if $container.len() != $size {
              None
      } else {
          use std::mem;
          let mut arr : [_; $size] = unsafe { mem::uninitialized() };
          for element in $container.into_iter().enumerate() {
              let old_val = mem::replace(&amp;mut arr[element.0],element.1);
              unsafe { mem::forget(old_val) };
          }
          Some(arr)
          }
      }};
  }
  
  fn array_to_vec(arr: &amp;[u8]) -&gt; Vec&lt;u8&gt; {
       let mut vector = Vec::new();
       for i in arr.iter() {
           vector.push(*i);
       }
       vector
  }
  
  fn vector_as_u8_4_array(vector: Vec&lt;u8&gt;) -&gt; [u8;4] {
      let mut arr = [0u8;4];
      for i in (0..4) {
          arr[i] = vector[i];
      }
      arr
  }
  </code></pre>