I'm trying to use the retain(...)
method to filter out values from a vector. As it stands, my code thus far is
let results = get_data(); // Returns Result<Vec<MyData>, Box<Error>>
match results {
Ok(mut my_data) => {
// Make sure that `name` is not None
myData.retain(|ref d| d.name.is_some());
// Only keep data if 'name' is in this list
let names = ["Alice", "Bob", "Claire"];
my_data.retain(|ref d| names.contains(d.name.unwrap().as_str()));
},
Err(_) => {}
}
However, this yields the error
|
| my_data.retain(|ref d| names.contains(d.name.unwrap().as_str()));
| ^^^^^^^^^^^^^^^^^^^^^^^^ expected &str, found str
|
= note: expected type `&&str`
found type `&str`
It seems like the compiler is telling me two separate things here, but in both cases it seems to want an additional reference. However, when I try to change the relevant line to &names.contains(d.name.unwrap().as_str())
, the compiler throws the following error.
|
| my_data.retain(|ref d| names.contains(&d.name.unwrap().as_str()));
| ^ cannot move out of borrowed content
How can I check whether a borrowed String
(I think that's what the type of name.unwrap()
is anyway) is contained in the names
vector?