title: In Rust, when boxing a value passed as a generic argument, why is a `'static` lifetime bound required?
body: <p>I'm trying to write a constructor function which takes a generic value implementing some trait by argument, and then boxes it (the point is to then initialize something with these boxes, but the following example is simplified):</p>
  <pre><code>struct Struct {}
  
  trait Trait {}
  
  impl Trait for Struct {}
  
  fn f(arg: impl Trait) -&gt; Box&lt;dyn Trait&gt; {
      Box::new(arg)
  }
  
  fn main() {
      let x = Struct {};
      f(x);
  }
  </code></pre>
  <p>Here, the compiler whines that <code>arg</code> might not live long enough. This makes perfect sense to me, because the only requirement on <code>arg</code> is <code>impl Trait</code>, meaning it might in fact be a reference which implement the trait, in which case it cannot be boxed safely.</p>
  <p>What confuses me is the following solution, adding a <code>'static</code> bound:</p>
  <pre><code>fn f(arg: impl Trait + 'static) -&gt; Box&lt;dyn Trait&gt; {
      Box::new(arg)
  }
  </code></pre>
  <p>Now, of course it works, but in principle we are now constrained to passing in values with a <code>'static</code> lifetime. Yet, the compiler lets me pass in <code>x</code> without a problem.</p>
  <p>Here are my questions, more specifically:</p>
  <ol>
  <li>Doesn't <code>x</code> have a lifetime, residing on the stack? Why is it possible to pass it to <code>f</code> when <code>arg</code> has a <code>'static</code> lifetime bound? Do these bounds only concern the lifetime of references?</li>
  <li>Is this solution valid in general, or will I face cases where the compiler will refuse my stack-allocated arguments?</li>
  <li>Is there a nicer way of saying &quot;any type that impls <code>Trait</code> where the type is not a reference?</li>
  </ol>
  <p>Note that 1) is mostly answered by <a href="https://stackoverflow.com/questions/61724455/why-does-rust-require-a-static-lifetime-for-this-variable">Why does Rust require a `&#39;static` lifetime for this variable?</a> but I am left confused as to if this is the idiomatic way of boxing arguments.</p>
  <hr />
  <p>EDIT:</p>
  <p>Now that I understand better, I am left wondering what is the idiomatic solution to fix the compiler error in the case of populating a struct:</p>
  <pre><code>struct OtherStruct {
      x: Box&lt;dyn Trait&gt;,
  }
  
  impl OtherStruct {
      fn new(arg: impl Trait) -&gt; Self { // Does not compile
          Self { x: Box::new(arg) }
      }
  }
  </code></pre>
  <p>The only solutions I see so far are 1) adding a lifetime parameter to <code>OtherStruct</code> (not so great), adding a <code>'static</code> lifetime bound to <code>arg</code> (I'm not sure if this is OK?)</p>