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()
).
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With