title: How to create and write to memory mapped files?
body: <blockquote>
    <p>Editor's note: This code example is from a version of Rust prior to 1.0 and the code it uses does not exist in Rust 1.0. Some answers have been updated to answer the core question for newer versions of Rust.</p>
  </blockquote>
  
  <p>I'm trying to create a memory mapped file using <code>std::os::MemoryMap</code>. The current approach looks as follows:</p>
  
  <pre><code>use std::os;
  use std::ptr;
  use std::old_io as io;
  use std::os::unix::prelude::AsRawFd;
  use std::os::MapOption;
  
  let path = Path::new("test.mmap");
  
  let f = match io::File::open_mode(&amp;path, io::Open, io::ReadWrite) {
      Ok(f) =&gt; f,
      Err(err) =&gt; panic!("Could not open file: {}", err),
  };
  
  let mmap_opts = &amp;[
      MapOption::MapReadable,
      MapOption::MapWritable,
      MapOption::MapFd(f.as_raw_fd())
  ];
  
  let mmap = match os::MemoryMap::new(1024*1024, mmap_opts) {
      Ok(mmap) =&gt; {
          println!("Successfully created the mmap: {}", mmap.len());
          mmap
      }
      Err(err) =&gt; panic!("Could not read the mmap: {}", err),
  };
  
  unsafe {
     let data = mmap.data();
  
      if data.is_null() {
          panic!("Could not access data from memory mapped file")
      }
  
      let src = "Hello!";
      ptr::copy_memory(data, src.as_ptr(), src.as_bytes().len());
  }
  </code></pre>
  
  <p>This program fails with</p>
  
  <pre class="lang-none prettyprint-override"><code>Process didn't exit successfully: `target/mmap` (status=4)
  </code></pre>
  
  <p>when calling <code>ptr::copy_memory</code> or any other operations on data.</p>
  
  <ul>
  <li>What is the reason I cannot write (or read) the data from the <code>MemoryMap</code>?</li>
  <li>What is the correct way to use <code>MemoryMap</code> in Rust?</li>
  </ul>