The compiler suggests I add a 'static lifetime because the parameter type may not live long enough, but I don't think that's what I want
<p>I'm trying to implement something that looks like this minimal example:</p>

<pre><code>trait Bar&lt;T&gt; {}

struct Foo&lt;T&gt; {
    data: Vec&lt;Box&lt;Bar&lt;T&gt;&gt;&gt;,
}

impl&lt;T&gt; Foo&lt;T&gt; {
    fn add&lt;U: Bar&lt;T&gt;&gt;(&amp;mut self, x: U) {
        self.data.push(Box::new(x));
    }
}
</code></pre>

<p>Since Rust defaults to (as far as I can tell) pass-by-ownership, my mental model thinks this should work.  The <code>add</code> method takes ownership of object <code>x</code> and is able to move this object into a <code>Box</code> because it knows the full type <code>U</code> (and not just trait <code>Bar&lt;T&gt;</code>).  Once moved into a <code>Box</code>, the lifetime of the item inside the box should be tied to the actual lifetime of the box (e.g., when <code>pop()</code>ed off the vector the object will be destroyed).</p>

<p>Clearly, however, the compiler disagrees (and I'm sure knows a bit more than I...), asking me to consider adding a <code>'static</code> lifetime qualifier (E0310).  I am 99% sure that's not what I want, but I'm not exactly sure what I'm supposed to do.</p>

<p>To clarify what I'm thinking and help identify misconceptions, my mental model, coming from a C++ background, is:</p>

<ul>
<li><code>Box&lt;T&gt;</code> is essentially <code>std::unique_ptr&lt;T&gt;</code></li>
<li>Without any annotations, variables are passed by value if <code>Copy</code> and rvalue-reference otherwise</li>
<li>With a reference annotation, <code>&amp;</code> is roughly <code>const&amp;</code> and <code>&amp;mut</code> is roughly <code>&amp;</code></li>
<li>The default lifetime is lexical scope</li>
</ul>