title: Creation of a hashmap with struct in Rust
body: <p>I am trying to set up a hashmap of objects / structs in rust... But I don't understand this concrete problem  (a lifetime error).</p>
  
  <pre><code>#[derive(Hash, Eq, PartialEq)]
  #[derive(Serialize, Deserialize, Debug)]
  pub struct Node&lt;'a&gt; {
      identifier: &amp;'a str,
      sha_id: Vec&lt;u8&gt;,
      successor_id: Option&lt;Vec&lt;u8&gt;&gt;,
      predecessor_id: Option&lt;Vec&lt;u8&gt;&gt;,
  }
  
  
  impl&lt;'a&gt; Node&lt;'a&gt; {
      ...
      ..
      .
  }
  
  pub struct Application&lt;'a&gt; {
      hash_map: HashMap&lt;&amp;'a str, Node&gt;,
  }
  
  impl&lt;'a&gt; Application&lt;'a&gt; {
      fn join(&amp;self, node: &amp;Node) {
          self.hash_map.insert(node.identifier, node);
      }
  }
  </code></pre>
  
  <p>The error is a missing lifetime specifier in  the <code>hash_map: HashMap&lt;&amp;'a str, Node&gt;</code> that I tried to solve changing Node to Node&lt;'a> but It throws a "mismatched type" error when I try to insert...</p>
  
  <p>I don't exactly why I have this problem missing the lifetime and I don't find solutions..</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <pre><code>#[derive(Hash, Eq, PartialEq)]
  #[derive(Serialize, Deserialize, Debug)]
  pub struct Node&lt;'a&gt; {
      identifier: &amp;'a str,
      sha_id: Vec&lt;u8&gt;,
      successor_id: Option&lt;Vec&lt;u8&gt;&gt;,
      predecessor_id: Option&lt;Vec&lt;u8&gt;&gt;,
  }
  
  
  impl&lt;'a&gt; Node&lt;'a&gt; {
      ...
      ..
      .
  }
  
  pub struct Application&lt;'a&gt; {
      hash_map: HashMap&lt;&amp;'a str, Node&lt;'a&gt;&gt;,
  }
  
  impl&lt;'a&gt; Application&lt;'a&gt; {
      fn join(&amp;self, node: &amp;Node) {
          self.hash_map.insert(node.identifier, *node);
      }
  }
  </code></pre>
  
  <p>And the output is:</p>
  
  <pre><code>"explicit lifetime required in the type of `node`"
  </code></pre>
  
  <p><strong>UPDATE2:</strong></p>
  
  <pre><code>pub struct Application&lt;'a&gt; {
      hash_map: HashMap&lt;&amp;'a str, Node&lt;'a&gt;&gt;,
  }
  
  impl&lt;'a&gt; Application&lt;'a&gt; {
      fn join(&amp;mut self, node: &amp;'a Node&lt;'a&gt;) {
          self.hash_map.insert(node.identifier, *node);
      }
  
  }
  </code></pre>
  
  <p>And the output is:</p>
  
  <pre><code>self.hash_map.insert(node.identifier, *node); cannot move out of borrowed content
  </code></pre>
  
  <p><strong>COMPLETE SOLUTION</strong></p>
  
  <pre><code>#[derive(Clone, Hash, Eq, PartialEq)]
  #[derive(Serialize, Deserialize, Debug)]
  pub struct Node&lt;'a&gt; {
      identifier: &amp;'a str,
      sha_id: Vec&lt;u8&gt;,
      successor_id: Option&lt;Vec&lt;u8&gt;&gt;,
      predecessor_id: Option&lt;Vec&lt;u8&gt;&gt;,
  }
  
  
  impl&lt;'a&gt; Node&lt;'a&gt; {
  ...
  ..
  .
  }
  
  pub struct Application&lt;'a&gt; {
      hash_map: HashMap&lt;&amp;'a str, Node&lt;'a&gt;&gt;,
  }
  
  impl&lt;'a&gt; Application&lt;'a&gt; {
      fn join(&amp;mut self, node: Node&lt;'a&gt;) {
          self.hash_map.insert(node.identifier, node);
      }
  
  }
  </code></pre>