title: How can I peek into a vector and pop if a condition is met?
body: <p>I want to retrieve an element from a vector if a condition on that element is true.</p>
  
  <pre><code>fn draw() -&gt; Option&lt;String&gt; {
      let mut v: Vec&lt;String&gt; = vec!["foo".to_string()];
      let t: Option&lt;String&gt;;
      let o = v.last();
  
      // t and v are actually a fields in a struct
      // so their lifetimes will continue outside of draw().
  
      match o {
          Some(ref e) =&gt; {
              if check(e) {
                  t = v.pop();
                  t.clone()
              } else {
                  None
              }
          }
          None =&gt; None,
      }
  }
  
  fn check(e: &amp;String) -&gt; bool {
      true
  }
  
  fn main() {}
  </code></pre>
  
  <p><a href="https://play.rust-lang.org/?gist=2f10be4f48247026a508f3e6679b533f&amp;version=stable" rel="noreferrer">playground</a></p>
  
  <p>This results in the error:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
    --&gt; src/main.rs:12:21
     |
  4  |     let o = v.last();
     |             - immutable borrow occurs here
  ...
  12 |                 t = v.pop();
     |                     ^ mutable borrow occurs here
  ...
  20 | }
     | - immutable borrow ends here
  </code></pre>
  
  <p>Which I sort of understand, however I don't see a way to end the borrow (without using <code>clone()</code>).</p>