title: Is functional programming in Rust zero-cost?
body: <p>I've made some tests on functional style programming performance in Rust:</p>
  
  <pre><code>extern crate rand; // 0.5.5
  
  use rand::Rng;
  
  fn time(f: impl FnOnce()) -&gt; std::time::Duration {
      let s = std::time::Instant::now();
      f();
      s.elapsed()
  }
  
  fn main() {
      let iteration = 10000000;
  
      let mut rng = rand::thread_rng();
      println!(
          "while: {:?}",
          time(|| {
              let mut i = 0;
              let mut num = 0i64;
              while i &lt; iteration {
                  num += rng.gen::&lt;i64&gt;();
                  i += 1;
              }
          })
      ); // 29.116528ms
  
      println!(
          "for: {:?}",
          time(|| {
              let mut num = 0i64;
              for _ in 0..iteration {
                  num += rng.gen::&lt;i64&gt;();
              }
          })
      ); // 26.68407ms
  
      println!(
          "fold: {:?}",
          time(|| {
              rng.gen_iter::&lt;i64&gt;().take(iteration).fold(0, |x, y| x + y);
          })
      ); // 26.065936ms
  }
  </code></pre>
  
  <p>I've set the optimization flag to compile it. </p>
  
  <p>These three cases took nearly the same time, does that mean that functional programming in Rust is zero-cost?</p>