title: Accessing hashmap field value as &mut inside struct impl
body: <p>given a simple struct like this:</p>
  
  <pre><code>struct Server {
    clients: HashMap&lt;usize, Client&gt;
  }
  </code></pre>
  
  <p>what is the best way to access <code>Client</code> as a <code>&amp;mut</code>? Consider the following code:</p>
  
  <pre><code>use std::collections::HashMap;
  
  struct Client {
    pub poked: bool
  }
  
  impl Client {
    pub fn poked(&amp;self) -&gt; bool {
      self.poked
    }
  
    pub fn set_poked(&amp;mut self) {
      self.poked = true;
    }
  }
  
  struct Server {
    clients: HashMap&lt;usize, Client&gt;
  }
  
  impl Server {
    pub fn poke_client(&amp;mut self, token: usize) {
      let client = self.clients.get_mut(&amp;token).unwrap();
      self.poke(client);
    }
  
    fn poke(&amp;self, c: &amp;mut Client) {
      c.set_poked();
    }
  }
  
  fn main() {
      let mut s = Server { clients: HashMap::new() };
      s.clients.insert(1, Client { poked: false });
  
      s.poke_client(1);
  
      assert!(s.clients.get(&amp;1).unwrap().poked() == true);
  }
  </code></pre>
  
  <p>The only two options I see are to use <code>RefCell</code>/<code>Cell</code> inside Client, which makes things look really horrible:</p>
  
  <pre><code>pub struct Client {
      nickname: RefCell&lt;Option&lt;String&gt;&gt;,
      username: RefCell&lt;Option&lt;String&gt;&gt;,
      realname: RefCell&lt;Option&lt;String&gt;&gt;,
      hostname: RefCell&lt;Option&lt;String&gt;&gt;,
      out_socket: RefCell&lt;Box&lt;Write&gt;&gt;,
  }
  </code></pre>
  
  <p>Or to wrap the <code>clients</code> in <code>RefCell</code>, which makes it impossible to have simple methods like this one for <code>Server</code>:</p>
  
  <pre><code>pub fn client_by_token(&amp;self, token: usize) -&gt; Option&lt;&amp;Client&gt; {
      self.clients_tok.get(&amp;token)
  }
  </code></pre>
  
  <p>forcing me to use closures (e.g. <code>with_client_by_token(|c| ...)</code>).</p>