title: How do I return a vector element from a Rust function?
body: <p>I would like to return an element of a vector:</p>
  
  <pre><code>struct EntryOne {
      pub name: String,
      pub value: Option&lt;String&gt;,
  }
  
  struct TestVec {}
  
  impl TestVec {
      pub fn new() -&gt; TestVec {
          TestVec {}
      }
  
      pub fn findAll(&amp;self) -&gt; Vec&lt;EntryOne&gt; {
          let mut ret = Vec::new();
          ret.push(EntryOne {
              name: "foo".to_string(),
              value: Some("FooVal".to_string()),
          });
          ret.push(EntryOne {
              name: "foo2".to_string(),
              value: Some("FooVal2".to_string()),
          });
          ret.push(EntryOne {
              name: "foo3".to_string(),
              value: None,
          });
          ret.push(EntryOne {
              name: "foo4".to_string(),
              value: Some("FooVal4".to_string()),
          });
  
          ret
      }
  
      pub fn findOne(&amp;self) -&gt; Option&lt;EntryOne&gt; {
          let mut list = &amp;self.findAll();
  
          if list.len() &gt; 0 {
              println!("{} elements found", list.len());
              list.first()
          } else {
              None
          }
      }
  }
  
  fn main() {
      let test = TestVec::new();
      test.findAll();
      test.findOne();
  }
  </code></pre>
  
  <p>(<a href="https://play.rust-lang.org/?gist=5eba731d9b15d177b5a6b2a35e4f2a47&amp;version=stable" rel="noreferrer">playground</a>)</p>
  
  <p>I always get this error:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0308]: mismatched types
    --&gt; src/main.rs:40:13
     |
  35 |     pub fn findOne(&amp;self) -&gt; Option&lt;EntryOne&gt; {
     |                              ---------------- expected `std::option::Option&lt;EntryOne&gt;` because of return type
  ...
  40 |             list.first()
     |             ^^^^^^^^^^^^ expected struct `EntryOne`, found &amp;EntryOne
     |
     = note: expected type `std::option::Option&lt;EntryOne&gt;`
                found type `std::option::Option&lt;&amp;EntryOne&gt;`
  </code></pre>
  
  <p>How do I return an element?</p>