title: How to use Itertools group_by iterator method without a for loop?
body: <p><a href="https://docs.rs/itertools/0.7.4/itertools/trait.Itertools.html#method.group_by" rel="noreferrer"><code>Itertools::group_by</code></a> is an iterator method that generates a new group every time the key function changes. The provided example shows how to use it with a <code>for</code> loop, but using the output <a href="https://docs.rs/itertools/0.7.4/itertools/structs/struct.GroupBy.html" rel="noreferrer"><code>GroupBy</code></a> struct in an iterator chain seems to be very cumbersome, unless I misunderstood something:</p>
  
  <pre><code>let data = vec![1, 3, -2, -2, 1, 0, 1, 2];
  
  // example from docs
  for (key, group) in &amp;data.into_iter().group_by(|elt| *elt &gt;= 0) {
      assert_eq!(4, group.sum::&lt;i32&gt;().abs());
  }
  
  // usage in an iterator method chain
  data.iter()
      .group_by(|elt| **elt &gt;= 0)
      .into_iter()
      .map(|bool, group| (bool, group.collect::&lt;Vec&lt;i32&gt;&gt;()))
      .collect::&lt;Vec&lt;(bool, Vec&lt;i32&gt;)&gt;&gt;();
  </code></pre>
  
  <p>The second example fails to compile:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0619]: the type of this value must be known in this context
    --&gt; src/main.rs:16:35
     |
  16 |         .map(|bool, group| (bool, group.collect::&lt;Vec&lt;i32&gt;&gt;()))
     |                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  
  error[E0599]: no method named `collect` found for type `std::iter::Map&lt;itertools::Groups&lt;'_, bool, std::slice::Iter&lt;'_, i32&gt;, [closure@src/main.rs:14:19: 14:35]&gt;, [closure@src/main.rs:16:14: 16:63]&gt;` in the current scope
    --&gt; src/main.rs:17:10
     |
  17 |         .collect::&lt;Vec&lt;(bool, Vec&lt;i32&gt;)&gt;&gt;();
     |          ^^^^^^^
     |
     = note: the method `collect` exists but the following trait bounds were not satisfied:
             `std::iter::Map&lt;itertools::Groups&lt;'_, bool, std::slice::Iter&lt;'_, i32&gt;, [closure@src/main.rs:14:19: 14:35]&gt;, [closure@src/main.rs:16:14: 16:63]&gt; : std::iter::Iterator`
             `&amp;mut std::iter::Map&lt;itertools::Groups&lt;'_, bool, std::slice::Iter&lt;'_, i32&gt;, [closure@src/main.rs:14:19: 14:35]&gt;, [closure@src/main.rs:16:14: 16:63]&gt; : std::iter::Iterator`
  
  error[E0593]: closure takes 2 arguments but 1 argument is required
    --&gt; src/main.rs:16:10
     |
  16 |         .map(|bool, group| (bool, group.collect::&lt;Vec&lt;i32&gt;&gt;()))
     |          ^^^ ------------------------------------------------- takes 2 arguments
     |          |
     |          expected closure that takes 1 argument
  </code></pre>
  
  <p>I'm not confident I understand this error, but I think the compiler wants the closure's <code>group</code> argument to have this explicit type:</p>
  
  <pre class="lang-none prettyprint-override"><code>itertools::Group&lt;'_, bool, std::slice::Iter&lt;'_, i32&gt;, [closure@src\main.rs:26:15: 26:31]&gt;
  </code></pre>
  
  <p>No one wants that in their code, so I hope I misunderstood.</p>
  
  <p>The question is, whether it's possible, and how to use <code>group_by</code> without a for loop?</p>