title: How do you convert an instance of generic T into a concrete instance in Rust?
body: <p>I'm trying to implement this pattern:</p>
  
  <pre><code>use std::any::Any;
  use std::fmt::Debug;
  
  trait CommandHandler&lt;TCommand&gt; {
      fn execute(&amp;self, data: TCommand);
  }
  
  #[derive(Debug)]
  struct FooCommand {}
  
  struct FooCommandHandler {}
  
  impl CommandHandler&lt;FooCommand&gt; for FooCommandHandler {
      fn execute(&amp;self, data: FooCommand) {
          println!("Foo");
      }
  }
  
  #[derive(Debug)]
  struct BarCommand {}
  
  struct BarCommandHandler {}
  
  impl CommandHandler&lt;BarCommand&gt; for BarCommandHandler {
      fn execute(&amp;self, data: BarCommand) {
          println!("Bar");
      }
  }
  
  fn execute&lt;T&gt;(command: T)
  where
      T: Any + Debug,
  {
      println!("Command: {:?}", command);
      match (&amp;command as &amp;Any).downcast_ref::&lt;FooCommand&gt;() {
          Some(c) =&gt; (FooCommandHandler {}).execute(c),
          None =&gt; {}
      };
      match (&amp;command as &amp;Any).downcast_ref::&lt;BarCommand&gt;() {
          Some(c) =&gt; (BarCommandHandler {}).execute(c),
          None =&gt; {}
      };
  }
  
  fn main() {
      (FooCommandHandler {}).execute(FooCommand {});
      (BarCommandHandler {}).execute(BarCommand {});
      execute(FooCommand {});
      execute(BarCommand {});
  }
  </code></pre>
  
  <p>This doesn't work:</p>
  
  <pre><code>error[E0308]: mismatched types
    --&gt; src/main.rs:37:51
     |
  37 |         Some(c) =&gt; (FooCommandHandler {}).execute(c),
     |                                                   ^ expected struct `FooCommand`, found &amp;FooCommand
     |
     = note: expected type `FooCommand`
                found type `&amp;FooCommand`
  
  error[E0308]: mismatched types
    --&gt; src/main.rs:41:51
     |
  41 |         Some(c) =&gt; (BarCommandHandler {}).execute(c),
     |                                                   ^ expected struct `BarCommand`, found &amp;BarCommand
     |
     = note: expected type `BarCommand`
                found type `&amp;BarCommand`
  </code></pre>
  
  <p>How can I implement the <code>execute()</code> method in a way that preserves the following requirements:</p>
  
  <ul>
  <li>The type <code>XCommand</code> should be totally naive of the <code>XCommandHandler</code>'s that execute it.</li>
  <li>Multiple implementations of <code>CommandHandler&lt;X&gt;</code> may exist.</li>
  <li>The command handler receives (and consumes) the concrete command instance, not a reference to it (making duplicate dispatch of commands impossible).</li>
  </ul>
  
  <p>In essence, I have a generic function <code>fn foo&lt;T&gt;(v: T)</code> and a I wish to dispatch to a number of concrete functions <code>fn foo1(v: Foo)</code>, <code>fn foo2(v: Bar)</code>; how do I do that?</p>
  
  <p>Is <code>transmute</code> the only option?</p>
  
  <p>Note that this is distinct from what <code>Any::downcast_ref</code> does, which is return an <code>&amp;Foo</code>, not <code>Foo</code> from the generic value v.</p>