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
}