title: Creating structures with references in Rust
body: <p>I am new to Rust and I have been trying to start solving some optimization problems using it. My goal is to parse an instance (in this case a JSON string) containing information about the entities of the problem. After parsing the instance, I would have a struct containing all the different types of entities read.</p>
  <p>In the following code, I have managed to read the JSON string and iterate over some entities. I tried to implement the trait <code>From</code> so that it could convert from a <code>serde_json::Map</code> to an <code>Entity</code>.</p>
  <pre class="lang-rust prettyprint-override"><code>use serde_json::{Map, Value};
  use std::{collections::HashSet};
  
  #[derive(Debug, PartialEq, Eq, Hash)]
  pub struct Entity&lt;'a&gt; {
      field1: u64,
      field2: &amp;'a str,
  }
  
  impl &lt;'a&gt; Entity&lt;'a&gt; {
      fn read_entities(json_entities: &amp;Value) -&gt; HashSet&lt;Entity&gt; {
          let mut entities = HashSet::new();
  
          if json_entities.is_array() {
              let array = json_entities.as_array().unwrap();
              for item in array.iter() {
                  entities.insert(Entity::from(item));
              }
          }
  
          entities
      }
  }
  
  impl &lt;'a&gt; From&lt;&amp;Map&lt;String, Value&gt;&gt; for Entity&lt;'a&gt; {
      fn from(obj: &amp;Map&lt;String, Value&gt;) -&gt; Entity&lt;'a&gt; {
          Entity {
              field1: obj[&quot;Field1&quot;].as_u64().unwrap(),
              field2: obj[&quot;Field2&quot;].as_str().unwrap(),
          }
      }
  }
  
  #[derive(Debug)]
  pub struct Data&lt;'a&gt; {
      entities: HashSet&lt;Entity&lt;'a&gt;&gt;,
  }
  
  impl &lt;'a&gt; Data&lt;'a&gt; {
      pub fn new() -&gt; Data&lt;'a&gt; {
          let data = r#&quot;{&quot;Entities&quot;:[{&quot;Field1&quot;:0,&quot;Field2&quot;:&quot;Abc&quot;},{&quot;Field1&quot;:1,&quot;Field2&quot;:&quot;Abc&quot;}]}&quot;#;
          let json: Value = serde_json::from_str(&amp;data).expect(&quot;&quot;);
          let mut entities = Entity::read_entities(&amp;json[&quot;Entities&quot;]);
          Data {
              entities,
          }
      }
  }
  
  fn main() {
      let data = Data::new();
  }
  </code></pre>
  <p>I have struggled with the following two errors:</p>
  <pre class="lang-rust prettyprint-override"><code>error[E0277]: the trait bound `Entity&lt;'_&gt;: From&lt;&amp;Value&gt;` is not satisfied
  error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements
  </code></pre>
  <p>My understanding from the first error is that the trait I have implemented is for <code>Entity&lt;'a&gt;</code> and thus the compiler cannot call <code>Entity::from(item)</code>. However, I could not figure out where I should inform the lifetime <code>'a</code> of <code>Entity</code> in this call. I feel that the second error is a similar problem regarding where I should place the lifetime <code>'a</code>. I have struggled with both errors for a while, but I could not figure out on my own.</p>
  <p>I would also be very glad if you could point out if anything is not idiomatic.</p>