title: How to return a reference to something inside a moved box?
body: <p>I want to store a Box in a map, and take the address of the contents of the Box and hand it out to C code. This is why I don't just store the value directly in the map, as map values move when a map grows.</p>
  <p>I'd like a function that returns a reference to the contents of the Box, that I can use in Rust code and either use as-is in Rust code, or convert the returned reference to a raw pointer for C.</p>
  <p>This code illustrates what I'm trying to do, but it doesn't work for obvious reasons. However, it's not clear to me how to fix it.</p>
  <pre><code>use std::collections::{HashMap};
  
  fn box_and_ref&lt;'a&gt;(map: &amp;'a mut HashMap&lt;String, Box&lt;Vec&lt;u8&gt;&gt;&gt;) -&gt; &amp;'a Vec&lt;u8&gt; {
      let v = vec!{b'h', b'e', b'l', b'l', b'o'};
      let b = Box::new(v);
      let r = b.as_ref();
      map.insert(&quot;foo&quot;.to_string(), b);
      r
  }
  
  fn main() {
      let mut map: HashMap&lt;String, Box&lt;Vec&lt;u8&gt;&gt;&gt; = HashMap::new();
  
      let v = box_and_ref(&amp;mut map);
      println!(&quot;{:?}&quot;, v);
  }
  </code></pre>
  <p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=8cc7f770603d7871753e592adb30de8b" rel="nofollow noreferrer">playground link</a></p>