title: How to get the number of elements (variants) in an enum as a constant value?
body: <p>Is there a way to extract the number of elements in an enum?</p>
  
  <p>Simple example (with imaginary <code>number_of_elements</code> method):</p>
  
  <pre><code>enum FooBar { A = 0, B, C, };
  
  println!("Number of items: {}", FooBar.number_of_elements());
  // "Number of items: 3"
  </code></pre>
  
  <p>In C I'd normally do... </p>
  
  <pre><code>enum FooBar { A = 0, B, C, };
  #define FOOBAR_NUMBER_OF_ITEMS (C + 1)
  </code></pre>
  
  <p>However the Rust equivalent to this doesn't work:</p>
  
  <pre><code>enum FooBar { A = 0, B, C, };
  const FOOBAR_NUMBER_OF_ITEMS: usize = (C as usize) + 1;
  
  // Raises an error:
  //     unimplemented constant expression: enum variants
  </code></pre>
  
  <p>Including the last item in the enum is very inconvenient because matching enums will error if all members aren't accounted for.</p>
  
  <pre><code>enum FooBar { A = 0, B, C, FOOBAR_NUMBER_OF_ITEMS, };
  </code></pre>
  
  <p>Is there a way to get the number of items in an enum as a constant value?</p>
  
  <hr>
  
  <p>Note: even though this isn't directly related to the question, the reason I was wanting this feature is I'm using the <a href="https://github.com/rust-unofficial/patterns/blob/master/patterns/builder.md" rel="noreferrer">builder-pattern</a> to construct a series of actions which only make sense to run once. For this reason I can use a fixed size array the size of the enum.</p>