title: How to set a variable to implementations of generic typed Trait in rust?
body: <p>I am new to rust and I am trying to write an app that basically uses one of many possible services to fetch some data, transform it and save to my database.</p>
  <p>I am trying to do something like a generic interface from Java, to instantiate the correct service based on command line input and use it throughout the program.</p>
  <p>I have coded something like this:</p>
  <pre class="lang-rust prettyprint-override"><code>use anyhow::Result;
  
  pub trait Service&lt;T&gt; {
      fn get_values(&amp;self, id: u64) -&gt; T;
      fn convert_data(&amp;self, input: T, output: &amp;mut Vec&lt;MyDataStruct&gt;) -&gt; Result&lt;String&gt;;
  }
  </code></pre>
  <p>and two different implementations:</p>
  <pre class="lang-rust prettyprint-override"><code>struct ServiceA {
      clientA: DataRetrieverForA,
  }
  
  struct ServiceB {
      clientB: DataRetrieverForB,
  }
  
  impl Service&lt;Result&lt;ADataStruct&gt;&gt; for ServiceA {
      fn get_values(&amp;self, id: u64) -&gt; Result&lt;ADataStruct&gt;;
      fn convert_data(&amp;self, input: Result&lt;ADataStruct&gt;, output: &amp;mut Vec&lt;MyDataStruct&gt;) -&gt; Result&lt;String&gt;;
  }
  
  impl Service&lt;BDataStruct&gt; for ServiceB {
      fn get_values(&amp;self, id: u64) -&gt; BDataStruct;
      fn convert_data(&amp;self, input: BDataStruct, output: &amp;mut Vec&lt;MyDataStruct&gt;) -&gt; Result&lt;String&gt;;
  }
  </code></pre>
  <p>the <code>self.get_values(id)</code> uses the <code>self.clientX</code> to retrieve data and <code>self.convert_data(input, &amp;mut output)</code> transforms data from <code>ADataStruct</code> and <code>BDataStruct</code> to <code>MyDataStruct</code> before saving it to my database.</p>
  <p>The app will run using either <code>ServiceA</code> or <code>ServiceB</code> depending on command line input:</p>
  <pre class="lang-rust prettyprint-override"><code>fn main() {
      // ...
  
      let service: Box&lt;&amp;(dyn Service&lt;_&gt;)&gt; = match cli_service {
          Service::A =&gt; { Box::new(&amp;ServiceA::new(...) }
          Service::B =&gt; { Box::new(&amp;ServiceB::new(...) }
      };
  
      //...
  }
  </code></pre>
  <p>I have tried many changes, mostly based on <a href="https://doc.rust-lang.org/book/ch10-02-traits.html" rel="nofollow noreferrer">https://doc.rust-lang.org/book/ch10-02-traits.html</a> and <a href="https://doc.rust-lang.org/book/ch17-02-trait-objects.html" rel="nofollow noreferrer">https://doc.rust-lang.org/book/ch17-02-trait-objects.html</a> but I can't find an example that handles functions that use a generic type from trait definition. When I removed the generic <em></em> parameter and fixed it to some common struct for testing, the code compiled and ran with no errors. So my guess is my mistake is with generic/trait usage.</p>
  <p>The error I get with this code:</p>
  <pre><code>
  error[E0277]: the trait bound `ServiceB: Service&lt;ADataStruct&gt;` is not satisfied
    --&gt; ori-runner\src\main.rs:40:37
     |
  40 | ... { Box::new(&amp;ServiceB::new(params)...
     |       -------- ^^^^^^^^^^^^^^^^^^^^^^ the trait `Service&lt;ADataStruct&gt;` is not implemented for `ServiceB`
     |       |
     |       required by a bound introduced by this call
     |
     = help: the following implementations were found:
               &lt;ServiceB as Service&lt;BDataStructure&gt;&gt;
     = note: required for the cast to the object type `dyn Service&lt;ADataStruct&gt;&gt;`
  
  </code></pre>
  <p>What am I doing wrong? It is obvious the first match type is defining the '_' of the dyn Service variable, but I am out of ideas and google searches...</p>
  <p>Thanks!</p>