title: How to create a binary file with Rust?
body: <p>I can write binary code to file with Rust. However, when I create a file, the file created is a text file, not binary.
  I can create a binary file with C++ like this:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>ofstream is("1.in", ofstream::binary | ofstream::out | ofstream:: trunc);
  </code></pre>
  
  <p>How about in Rust? This is my attempt:</p>
  
  <pre><code>struct IndexDataStructureInt {
      row: u32,
      key_value: u32,
  }
  
  let mut index_arr: Vec&lt;IndexDataStructureInt&gt; = Vec::new();
  // doing something push 100 IndexDataStructureInt to index_arr
  let mut fileWrite = File::create(tableIndexName).unwrap();
  for i in 0..index_arr.len() {
      write!(
          fileWrite,
          "{:b}{:b}",
          index_arr[i].row, index_arr[i].key_value
      );
  }
  </code></pre>
  
  <p>After running this code, it writes 200 u32 integer binary number to the file <code>tableIndexName</code>. However, the file size is not 800bytes. It is about 4KB.</p>