I have an array defined in the form of a constant, outside any function:
const VALUES: [(char, &str); 2] = [('A', "abc"), ('B', "acb")];
I'm attempting to use the find()
method located on the Iterator
, in order to extract a single element from an array based on a predicate:
VALUES.iter().find(|&&(name, _)| name == 'A');
In this form, it works fine. I am, however, unable to evaluate the found element into anything, as as soon as I attempt creating a let
binding, trying to bind the result, which, according to the docs should come back as an Option<T>
.
Let's change the second line to that which doesn't work:
const VALUES: [(char, &str); 2] = [('A', "abc"), ('B', "acb")];
fn main() {
let result = VALUES.iter().find(|&&(name, _)| name == 'A');
}
One would expect this to return the Option<T>
as according to the docs, but instead I get back a compilation error:
error: borrowed value does not live long enough
--> src/main.rs:4:63
|
4 | let result = VALUES.iter().find(|&&(name, _)| name == 'A');
| ------ temporary value created here ^ temporary value dropped here while still borrowed
5 | }
| - temporary value needs to live until here
|
= note: consider using a `let` binding to increase its lifetime
I'm utterly confused; I'm certain I've just messed up with the "borrow checker". Perhaps someone can point me in the right direction?