title: How can I box the contents of an iterator of a type that implements a trait?
body: <p>I'm taking an iterator of some type that must implement the trait <code>A</code>, and trying to convert it into a <code>Vec</code> of <code>Box</code>es of that trait:</p>
  
  <pre><code>trait A {}
  
  fn test2&lt;'a, I&gt;(iterator: I) -&gt; Vec&lt;Box&lt;A + 'a&gt;&gt;
  where
      I: IntoIterator,
      I::Item: A + 'a,
  {
      iterator
          .into_iter()
          .map(|a| Box::new(a))
          .collect::&lt;Vec&lt;Box&lt;A + 'a&gt;&gt;&gt;()
  }
  </code></pre>
  
  <p>However, this fails to compile, saying:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0277]: the trait bound `std::vec::Vec&lt;std::boxed::Box&lt;A + 'a&gt;&gt;: std::iter::FromIterator&lt;std::boxed::Box&lt;&lt;I as std::iter::IntoIterator&gt;::Item&gt;&gt;` is not satisfied
    --&gt; src/main.rs:11:10
     |
  11 |         .collect::&lt;Vec&lt;Box&lt;A + 'a&gt;&gt;&gt;()
     |          ^^^^^^^ a collection of type `std::vec::Vec&lt;std::boxed::Box&lt;A + 'a&gt;&gt;` cannot be built from an iterator over elements of type `std::boxed::Box&lt;&lt;I as std::iter::IntoIterator&gt;::Item&gt;`
     |
     = help: the trait `std::iter::FromIterator&lt;std::boxed::Box&lt;&lt;I as std::iter::IntoIterator&gt;::Item&gt;&gt;` is not implemented for `std::vec::Vec&lt;std::boxed::Box&lt;A + 'a&gt;&gt;`
     = help: consider adding a `where std::vec::Vec&lt;std::boxed::Box&lt;A + 'a&gt;&gt;: std::iter::FromIterator&lt;std::boxed::Box&lt;&lt;I as std::iter::IntoIterator&gt;::Item&gt;&gt;` bound
  </code></pre>
  
  <p>This error kind of makes sense, but then I don't see why there's no problem with the following:</p>
  
  <pre><code>fn test&lt;'a, T: A + 'a&gt;(t: T) -&gt; Box&lt;A + 'a&gt; {
      Box::new(t)
  }
  </code></pre>
  
  <p>How is that any different? How can I express that I'd like to <code>Box</code> them as <code>A</code>s, rather than whatever type they may be?</p>