title: How can I use Itertools::group_by without cloning or the error "cannot move out of a value which is behind a shared reference"?
body: <p>I have what seems to me like a simple <code>Itertools::group_by</code> task, but I can't make it compile unless I add <code>clone</code> calls.</p>
  
  <pre class="lang-rust prettyprint-override"><code>use itertools::Itertools; // 0.9.0;
  use std::collections::HashMap;
  
  #[derive(Debug, Clone, PartialEq, Eq)]
  pub struct Server {
      pub desc: String,
      pub group_name: Option&lt;String&gt;,
  }
  
  fn main() {
      let servers = vec![
          Server {
              desc: "one".to_string(),
              group_name: Some("A".to_string()),
          },
          Server {
              desc: "two".to_string(),
              group_name: Some("A".to_string()),
          },
          Server {
              desc: "three".to_string(),
              group_name: None,
          },
      ];
  
      let mut servers_by_group_name = HashMap::new();
      for (key, group) in &amp;servers.into_iter().group_by(|s| s.group_name) {
          servers_by_group_name.insert(key, group.collect::&lt;Vec&lt;_&gt;&gt;());
      }
  }
  </code></pre>
  
  <p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=23a8282ec1b8a2a6b2741b8d0e8c591f" rel="nofollow noreferrer">playground</a></p>
  
  <p>The error is:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0507]: cannot move out of `s.group_name` which is behind a shared reference
    --&gt; src/main.rs:27:59
     |
  27 |     for (key, group) in &amp;servers.into_iter().group_by(|s| s.group_name) {
     |                                                           ^^^^^^^^^^^^
     |                                                           |
     |                                                           move occurs because `s.group_name` has type `std::option::Option&lt;std::string::String&gt;`, which does not implement the `Copy` trait
     |                                                           help: consider borrowing the `Option`'s content: `s.group_name.as_ref()`
  </code></pre>
  
  <p>The error is quite clear to me. I know I could make it go away by cloning the <code>s.group_name</code> in the <code>group_by</code>. I believe I could also use <code>.iter()</code> instead of <code>.into_iter()</code>, then it would possibly work, but then I would have a <code>Vec&lt;&amp;Server&gt;</code> in the values of my map, but I'd really like a <code>Vec&lt;Server&gt;</code> (because my next step is wrapping that <code>Server</code> in a new datastructure and I really want the ownership).</p>
  
  <p>Is there a way to implement this without cloning the <code>group_name</code> in the <code>group_by</code> closure?</p>