How can I reuse a box that I have moved the value out of?
<p>I have some non-copyable type and a function that consumes and (maybe) produces it:</p>

<pre><code>type Foo = Vec&lt;u8&gt;;

fn quux(_: Foo) -&gt; Option&lt;Foo&gt; {
    Some(Vec::new())
}
</code></pre>

<p>Now consider a type that is somehow conceptually very similar to <code>Box</code>:</p>

<pre><code>struct NotBox&lt;T&gt; {
    contents: T
}
</code></pre>

<p>We can write a function that temporarily moves out contents of the <code>NotBox</code> and puts something back in before returning it:</p>

<pre><code>fn bar(mut notbox: NotBox&lt;Foo&gt;) -&gt; Option&lt;NotBox&lt;Foo&gt;&gt; {
    let foo = notbox.contents; // now `notbox` is "empty"
    match quux(foo) {
        Some(new_foo) =&gt; {
            notbox.contents = new_foo; // we put something back in
            Some(notbox)
        }
        None =&gt; None
    }
}
</code></pre>

<p>I want to write an analogous function that works with <code>Box</code>es but the compiler does not like it:</p>

<pre><code>fn baz(mut abox: Box&lt;Foo&gt;) -&gt; Option&lt;Box&lt;Foo&gt;&gt; {
    let foo = *abox; // now `abox` is "empty"
    match quux(foo) {
        Some(new_foo) =&gt; {
            *abox = new_foo; // error: use of moved value: `abox`
            Some(abox)
        }
        None =&gt; None
    }
}
</code></pre>

<p>I could return <code>Some(Box::new(new_foo))</code> instead but that performs unnecessary allocation - I already have some memory at my disposal! Is it possible to avoid that?</p>

<p>I would also like to get rid of the <code>match</code> statements but again the compiler is not happy with it (even for the <code>NotBox</code> version):</p>

<pre><code>fn bar(mut notbox: NotBox&lt;Foo&gt;) -&gt; Option&lt;NotBox&lt;Foo&gt;&gt; {
    let foo = notbox.contents;
    quux(foo).map(|new_foo| {
        notbox.contents = new_foo; // error: capture of partially moved value: `notbox`
        notbox
    })
}
</code></pre>

<p>Is it possible to work around that?</p>