title: How to match trait implementors
body: <p>I have a trait that is implemented by some structs. I want to write a pattern match where I can handle every possible case:</p>
  
  <pre class="lang-rust prettyprint-override"><code>trait Base {}
  
  struct Foo {
      x: u32,
  }
  struct Bar {
      y: u32,
  }
  
  impl Base for Foo {}
  impl Base for Bar {}
  
  fn test(v: bool) -&gt; Box&lt;Base + 'static&gt; {
      if v {
          Box::new(Foo { x: 5 })
      } else {
          Box::new(Bar { y: 10 })
      }
  }
  
  fn main() {
      let f: Box&lt;Base&gt; = test(true);
  
      match *f {
          Foo { x } =&gt; println!("it was Foo: {}!", x),
          Bar { y } =&gt; println!("it was Bar: {}!", y),
      }
  }
  </code></pre>
  
  <p>(<a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2015&amp;gist=2d62527955b5c72dd94d892c546cbf3a" rel="noreferrer">Playground</a>)</p>
  
  <p>I'm getting this compilation error:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0308]: mismatched types
    --&gt; src/main.rs:25:9
     |
  25 |         Foo { x } =&gt; println!("it was Foo: {}!", x),
     |         ^^^^^^^^^ expected trait Base, found struct `Foo`
     |
     = note: expected type `dyn Base`
                found type `Foo`
  
  error[E0308]: mismatched types
    --&gt; src/main.rs:26:9
     |
  26 |         Bar { y } =&gt; println!("it was Bar: {}!", y),
     |         ^^^^^^^^^ expected trait Base, found struct `Bar`
     |
     = note: expected type `dyn Base`
                found type `Bar`
  </code></pre>