title: Rust impl Trait as function return type
body: <p>I have the following function in which <code>Command</code> is a trait</p>
  <pre><code>pub fn parse_arguments(matches: ArgMatches) -&gt; Result&lt;impl Command, &amp;'static str&gt;
  </code></pre>
  <p>Inside the function body, I want to return different implementations of Command depending on the arguments
  like</p>
  <pre><code>match args.subcommand() {
      (&quot;init&quot;, Some(args)) =&gt; {
          Ok(Init::new(args))
      }
      (&quot;btc&quot;, Some(args)) =&gt; {
          Ok(ImportBtc::new(args))
      },
      (&quot;grin&quot;, Some(args)) =&gt; {
          Ok(ImportGrin::new(args))
      },
      _ =&gt; Err (&quot;Invalid subcommand supplied&quot;)
  }
  </code></pre>
  <p>Compilation will fail with an error:</p>
  <pre><code>expected struct `commands::cmd_types::Init`, found struct `commands::cmd_types::ImportBtc`
  </code></pre>
  <p>and similar for the <code>ImportGrin</code> return line.</p>
  <p>Have I misunderstood how <code>impl Trait</code> works?</p>