title: What is a faster way to iterate through the bytes of a file in Rust?
body: <p>I'm new to Rust and I'm trying to come up with a simple backup program. In a first step, the files are broken down into blocks of variable length (via <a href="https://csyhua.github.io/csyhua/hua-atc2016.pdf" rel="nofollow noreferrer">content-defined chunking</a>).</p>
  <p>To do this, I have to read the file byte by byte. Unfortunately, I find that the process is terribly slow. With <code>dd</code> I can read at up to 350 MiB / s. Nevertheless, I only get about 45 MiB / s with the following Rust code. (<em>I left out all the chunking stuff there.</em>)</p>
  <p>The file I am reading is around 7.7 GiB in size.</p>
  <pre><code>// main.rs
  
  
  use std::fs::File;
  use std::io::BufReader;
  use std::io::{Read, Bytes};
  use std::time::{Instant, Duration};
  
  fn main() {
      let file = File::open(&quot;something_big.tar&quot;).expect(&quot;Cannot read file.&quot;);
      
      let mut buf = BufReader::new(file);
  
      let mut x = 0u8;
  
      let mut num_bytes = 0usize;
  
      let t1 = Instant::now();
  
      for b in buf.bytes() {
          match b {
              Ok(b) =&gt; {
                  x += b;
                  num_bytes += 1;
                  // chunking stuff omitted
              },
              Err(_) =&gt; panic!(&quot;I/O Error&quot;)
          }
      }
  
      let dur = t1.elapsed().as_secs_f64();
      let mut num_bytes = (num_bytes as f64) / 1_048_576f64;
  
      println!(&quot;RESULT: {}&quot;, x);
  
      println!(&quot;Read speed: {:.1} MiB / s&quot;, num_bytes / dur);
  }
  
  </code></pre>
  <p><strong>Question</strong>: What is a better way to <em>quickly</em> iterate through the bytes of a file with Rust? And what is wrong with my code?</p>
  <p>I know that maybe I could use the <code>memmap</code> crate or something like this – but: I don't want to do that.</p>