title: How to get an enum reference from a reference to a child type of the enum?
body: <p>I have an enum type <code>Shape</code> that contains two types <code>Rect</code> and <code>Circle</code>. now there is a function that can find <code>&amp;Rect</code> or <code>&amp;Circle</code> objects, how can I get them to return as <code>&amp;Shape</code>?</p>
  <pre class="lang-rust prettyprint-override"><code>#[derive(Debug)]
  struct Rect {}
  
  #[derive(Debug)]
  struct Circle {}
  
  #[derive(Debug)]
  enum Shape {
      R(Rect),
      C(Circle),
  }
  
  struct Container {
      rects: Vec&lt;(usize, Rect)&gt;,
      circls: Vec&lt;(usize, Circle)&gt;,
  }
  
  impl Container {
      fn find_shape(&amp;self, id: usize) -&gt; Option&lt;&amp;Shape&gt; {
          let optr = self.rects.iter().find(|x| x.0 == id);
          if let Some((_, r)) = optr {
              return Some(&amp;Shape::R(r)); // expected struct `Rect`, found `&amp;Rect`
          }
          let optc = self.circls.iter().find(|x| x.0 == id);
          if let Some((_, c)) = optc {
              return Some(c); // expected enum `Option&lt;&amp;Shape&gt;` found reference `&amp;Circle`
          }
          return None;
      }
  }
  
  fn main() {
      let cont = Container {
          rects: vec![(0, Rect {}), (2, Rect {})],
          circls: vec![(1, Circle {}), (3, Circle {})],
      };
      let s = cont.find_shape(1).unwrap();
      println!(&quot;shape is {:?}&quot;, s);
  }
  </code></pre>