title: Moving out of a function both owned and borrowed entities?
body: <p>What is proper way of doing that? What options are available? I am aware about the problem of dangled references and the problem of self-referential struct. Still I have strong intuition it's a reachable problem because both owner and borrowed reference are returned and no memory deallocation happen. Also it's quite general problem! In theory, it's a solvable one.</p>
  <pre class="lang-rust prettyprint-override"><code>use byte_slice_cast::*;
  
  fn main() {
      let context = context_make();
      dbg!(&amp;context);
  }
  
  //
  
  fn context_make&lt;'a&gt;() -&gt; Context&lt;'a&gt; {
      Context::&lt;'a&gt;::new()
  }
  
  //
  
  #[derive(Debug)]
  struct Context&lt;'a&gt; {
      pub dst_buffer: Box&lt;[f32]&gt;,
      pub dst_buffer_bytes: &amp;'a [u8],
  }
  
  //
  
  impl&lt;'a&gt; Context&lt;'a&gt; {
      fn new() -&gt; Context&lt;'a&gt; {
          let len: usize = 13;
          let dst_buffer: Box&lt;[f32]&gt; = vec![0_f32; len].into_boxed_slice();
          let dst_buffer_bytes = dst_buffer.as_byte_slice();
          Context {
              dst_buffer,
              dst_buffer_bytes,
          }
      }
  }
  
  </code></pre>
  <p>Note: this code requires <code>byte-slice-cast = &quot;1.2.0&quot;</code></p>
  <p>Interesting to compare solutions if there are more than one alternatives.</p>
  <p><a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2021&amp;gist=af0b6da57a5d4153249f79f403d4d13e" rel="nofollow noreferrer">Playground</a></p>