title: Copy a struct to a specific memory location in Rust
body: <p>If I have a <code>struct</code>, for example:</p>
  
  
  
  <pre class="lang-rs prettyprint-override"><code>#[derive(Clone, Copy)]
  #[repr(C, packed)]
  pub struct SomeData {
      a: u16,
      b: u64,
      c: u32,
      d: u16,
  }
  </code></pre>
  
  <p>How do I copy it to a specific location in memory, e.g. to a point <code>0x1000</code> in memory efficiently? Would something like this work?</p>
  
  <pre class="lang-rs prettyprint-override"><code>let dst_addr: u64 = 0x1000;
  let src = SomeData {a: 1, b: 2, c: 3, d: 4};
  unsafe {
      let tmp: &amp;[u8; 10] = transmute(src);
      copy(dst_addr as *mut _, tmp);
  }
  </code></pre>
  
  <p><em>Please note that the <code>repr(C, packed)</code> part is actually needed here.</em></p>
  
  <p>The software is running on bare x86_64, <strong>ring 0</strong>, without operating system or other limitations. Also, I'm programming <strong>without the standard library</strong>, so this should be achievable with only the <code>core</code> library.</p>
  
  <p>This is, of course, unsafe, but it is not a problem.</p>
  
  <p><strong>Edit</strong>: Just clarifying: I'm copying to uninitialized memory.</p>