title: How can I convert the lower/upper 8 bits of a u16 to a u8 in Rust?
body: <p>I want to convert a <code>u16</code> to two separate <code>u8</code>s. I tried to use some bit masks:</p>
  
  <pre><code>use std::convert::From;
  
  fn main() {
      let n1: u8 = 0x41;
      let n2: u16 = 0x4157;
  
      println!("Number:{}", char::from(n1));
  
      let b1: u8 = n2 &amp; 0xFF;
      let b2: u8 = n2 &gt;&gt; 8;
  
      println!("b1: {}", b1);
      println!("b2: {}", b2);
  }
  </code></pre>
  
  <pre class="lang-none prettyprint-override"><code>error[E0308]: mismatched types
   --&gt; src/main.rs:9:18
    |
  9 |     let b1: u8 = n2 &amp; 0xFF;
    |                  ^^^^^^^^^ expected u8, found u16
  
  error[E0308]: mismatched types
    --&gt; src/main.rs:10:18
     |
  10 |     let b2: u8 = n2 &gt;&gt; 8;
     |                  ^^^^^^^ expected u8, found u16
  </code></pre>
  
  <p>This question is not <em>why does the compiler raise a mismatched type error?</em>, rather, it is <em>How can I convert the lower/upper 8 bits of a <code>u16</code> to a <code>u8</code> in Rust?</em>. Potentially, there are other ways to do this and this question does not constrain the answer to the <code>as</code> keyword.</p>