title: Splitting a `Vec`
body: <p>I'm trying to write a little buffer-thing for parsing so I can pull records off the front of as I parse them out, ideally without making any copies and just transferring ownership of chunks of the front of the buffer off as I run. Here's my implementation:</p>
  
  <pre><code>struct BufferThing {
      buf: Vec&lt;u8&gt;,
  }
  
  impl BufferThing {
      fn extract(&amp;mut self, size: usize) -&gt; Vec&lt;u8&gt; {
          assert!(size &lt;= self.buf.len());
          let remaining: usize = self.buf.len() - size;
          let ptr: *mut u8 = self.buf.as_mut_ptr();
  
          unsafe {
              self.buf = Vec::from_raw_parts(ptr.offset(size as isize), remaining, remaining);
              Vec::from_raw_parts(ptr, size, size)
          }
      }
  }
  </code></pre>
  
  <p>This compiles, but panics with a <code>signal: 11, SIGSEGV: invalid memory reference</code> as it starts running. This is mostly the same code as the example in <a href="https://doc.rust-lang.org/nomicon/borrow-splitting.html" rel="noreferrer">the Nomicon</a>, but I'm trying to do it on <code>Vec</code>'s and I'm trying to split a field instead of the object itself.</p>
  
  <p>Is it possible to do this without copying out one of the <code>Vec</code>s? And is there some section of the Nomicon or other documentation that explains why I'm blowing everything up in the <code>unsafe</code> block?</p>