title: How do I implement a trait for an enum and its respective variants?
body: <p>I'm trying to use enum variants to capture data which is heterogeneous in nature (has different collections of fields) but which is of the same "type" from a protocol perspective.  However, I'm not sure how to implement subtype-specific methods and traits.  Here is a minimal example of how I can create an enumeration of Data and I can use enum variant constructors to specify the types, but if I implement a trait on the variant, calling that function is not something I've figured out how to do.</p>
  
  <pre><code>use std::fmt;
  
  enum Data {
      N(NData),
      S(SData),
  }
  
  struct NData {
      numeric: u32,
  }
  
  impl fmt::Display for NData {
      fn fmt(&amp;self, f: &amp;mut fmt::Formatter) -&gt; fmt::Result {
          write!(f, "{}", self.numeric)
      }
  }
  
  struct SData {
      stringy: Vec&lt;String&gt;,
  }
  
  fn main() {
      let d_n: Data = Data::N(NData { numeric: 0x0 });
      let n = NData { numeric: 0xff };
  
      // Fails, fmt::Display not implemented for Data
      println!("{}", d_n);
  
      // Just fine!
      println!("{}", n);
  }
  </code></pre>