title: What is the most appropriate way to convert nibbles to a u64?
body: <p>I am trying to learn Rust and decided to write a program that converts a hex string into a u64. </p>
  
  <p>Currently, I have parsed the string into a vector of u8 values, each representing four bits (or "nibble"). I wrote the following code to take a <code>Vec&lt;u8&gt;</code> and return a corresponding <code>u64</code>. It works (as far as my testing shows), but I am not sure if it is the "appropriate" way in Rust to go about doing this.</p>
  
  <pre><code>fn convert_nibbles_to_u64(values: &amp;Vec&lt;u8&gt;) -&gt; u64 {
      // We need to turn this buffer into a u64 now
      let mut temp:u64 = 0;
      for i in values {
          temp = temp &lt;&lt; 4;
  
          unsafe {
              // We need to unsafely convert a u8 to a u64. Note that
              // the host endian-ness will matter here.
              use std::mem;
              let i_64_buffer = [0u8,0u8,0u8,0u8,0u8,0u8,0u8,i.clone()];
              let i_64 = mem::transmute::&lt;[u8; 8], u64&gt;(i_64_buffer);
              let i_64_be = u64::from_be(i_64);
              temp = temp | i_64_be;           
          }
      }
      return temp;
  }
  </code></pre>
  
  <p>I suppose the main issue I don't know how else to cast a <code>u8</code> to a <code>u64</code> value. Could you comment on ways to improve or write the code in a more idiomatic, Rust-like style?</p>
  
  <p>EDIT:
  I have tried the following (unsuccessful) alternatives to the unsafe block:</p>
  
  <p>Or'ing with <code>i</code> as a <code>u64</code>:</p>
  
  <pre><code>temp = temp | i as u64; 
  ------
  Compiler error:
  main.rs:115:23: 115:31 error: non-scalar cast: `&amp;u8` as `u64`
  main.rs:115         temp = temp | i as u64;
  </code></pre>
  
  <p>Or'ing with <code>i</code> directly:</p>
  
  <pre><code>temp = temp | i;
  ------
  Compiler error:
  main.rs:115:16: 115:24 error: the trait `core::ops::BitOr&lt;&amp;u8&gt;` is not implemented for the type `u64` [E0277]
  main.rs:115         temp = temp | i;
  </code></pre>