title: How do you modify a file's contents instead of prepending to the file in Rust?
body: <p>I thought you just open a file and seek to the position you want to overwrite and start writing, but it seems that it only adds bytes at that position. How to I remove bytes from a file or overwrite bytes?</p>
  
  <p>An example:</p>
  
  <pre><code>use std::fs::OpenOptions;
  use std::io::{prelude::*, Seek, SeekFrom};
  
  fn main() {
      let mut file = OpenOptions::new()
          .read(true)
          .append(true)
          .create(true)
          .open("/tmp/file.db")
          .unwrap();
  
      let bytes: [u8; 4] = [1, 2, 3, 4];
  
      file.seek(SeekFrom::Start(0)).unwrap();
      file.write_all(&amp;bytes).unwrap();
  }
  </code></pre>
  
  <p>Output file before:</p>
  
  <pre class="lang-none prettyprint-override"><code>00000000: 0102 0304 0a                             .....
  </code></pre>
  
  <p>Output file after:</p>
  
  <pre class="lang-none prettyprint-override"><code>00000000: 0102 0304 0102 0304 0a                   .........
  </code></pre>
  
  <p>As you can see, seeking to 0 does not overwrite the 4 bytes already in the file. Instead it prepends them to the file.</p>