title: How to match on data type in Rust?
body: <p>I'm trying to match on the datatype of a generic field of a struct and react accordingly. My general idea was like this (code doesn't compile):</p>
  
  <pre><code>struct Foo&lt;T&gt; {
      bar: T,
  }
  
  fn main() {
      let x = Foo::&lt;String&gt; {
          bar: "world".to_string(),
      };
  
      match x.bar {
          String =&gt; println!("It's a string!"),
          u32 =&gt; println!("It's a u32!"),
          _ =&gt; println!("Something else"),
      };
  
      println!("end of program!");
  }
  </code></pre>
  
  <p>The error message from the compiler:</p>
  
  <pre class="lang-none prettyprint-override"><code>warning: unreachable pattern
    --&gt; src/main.rs:12:9
     |
  11 |         String =&gt; println!("It's a string!"),
     |         ------ matches any value
  12 |         u32 =&gt; println!("It's a u32!"),
     |         ^^^ unreachable pattern
     |
     = note: `#[warn(unreachable_patterns)]` on by default
  
  warning: unreachable pattern
    --&gt; src/main.rs:13:9
     |
  11 |         String =&gt; println!("It's a string!"),
     |         ------ matches any value
  12 |         u32 =&gt; println!("It's a u32!"),
  13 |         _ =&gt; println!("Something else"),
     |         ^ unreachable pattern
  
  warning: unused variable: `String`
    --&gt; src/main.rs:11:9
     |
  11 |         String =&gt; println!("It's a string!"),
     |         ^^^^^^ help: consider prefixing with an underscore: `_String`
     |
     = note: `#[warn(unused_variables)]` on by default
  
  warning: unused variable: `u32`
    --&gt; src/main.rs:12:9
     |
  12 |         u32 =&gt; println!("It's a u32!"),
     |         ^^^ help: consider prefixing with an underscore: `_u32`
  
  warning: variable `String` should have a snake case name
    --&gt; src/main.rs:11:9
     |
  11 |         String =&gt; println!("It's a string!"),
     |         ^^^^^^ help: convert the identifier to snake case: `string`
     |
     = note: `#[warn(non_snake_case)]` on by default
  </code></pre>
  
  <p>What I wanted was for <code>x</code> to match the first one. I'm actually not sure what I want to do can be done, but what would achieve the desired effect?</p>