title: What is the safe way to convert a Vec<T> to a &[T]
body: <p>How can I assign to a lifetime to a vector's underlying heap allocation without moving it in memory?</p>
  
  <p>The easiest way I've found to do this is</p>
  
  <pre><code>pub fn heap_slice&lt;'a, T:'a&gt;(x: Vec&lt;T&gt;) -&gt; &amp;'a [T] {
      use std::mem::forget;
      use std::slice::from_raw_parts;
      x.shrink_to_fit();
      let ptr = x.ptr();
      let len = x.len();
      forget(x);
      unsafe{ from_raw_parts(ptr,len) };
  }
  </code></pre>
  
  <p>But this does call <code>unsafe</code> outside of the stdlib, which is generally frowned upon. Is there a safe way?</p>
  
  <p>I see the <code>into_boxed_slice</code> is doing roughly exact same thing. But how do I recover the <code>&amp;[T]</code> and assign it a lifetime once it is a <code>Box&lt;[T]&gt;</code>?</p>