Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I peek into a vector and pop if a condition is met?

Tags:

rust

I want to retrieve an element from a vector if a condition on that element is true.

fn draw() -> Option<String> {
    let mut v: Vec<String> = vec!["foo".to_string()];
    let t: Option<String>;
    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) => {
            if check(e) {
                t = v.pop();
                t.clone()
            } else {
                None
            }
        }
        None => None,
    }
}

fn check(e: &String) -> bool {
    true
}

fn main() {}

playground

This results in the error:

error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
  --> 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

Which I sort of understand, however I don't see a way to end the borrow (without using clone()).

like image 466
Kreisquadratur Avatar asked Sep 17 '15 15:09

Kreisquadratur


1 Answers

Borrows are lexical, so v has the life-time of the whole function. If one can reduce the scope, one can use last() and pop(). One way to do this is a functional-style map:

let mut v: Vec<String> = vec!["foo".to_string()];
if v.last().map_or(false, check) {
    v.pop()
} else {
    None
}
like image 54
Kreisquadratur Avatar answered Nov 16 '22 12:11

Kreisquadratur