I am trying to filter a Vec<Vocabulary>
where Vocabulary
is a custom struct
, which itself contains a struct
VocabularyMetadata
and a Vec<Word>
:
#[derive(Serialize, Deserialize)]
pub struct Vocabulary {
pub metadata: VocabularyMetadata,
pub words: Vec<Word>
}
This is for handling a route in a web application, where the route looks like this: /word/<vocabulary_id>/<word_id>
.
Here is my current code trying to filter
the Vec<Vocabulary>
:
let the_vocabulary: Vec<Vocabulary> = vocabulary_context.vocabularies.iter()
.filter(|voc| voc.metadata.identifier == vocabulary_id)
.collect::<Vec<Vocabulary>>();
This does not work. The error I get is:
the trait `std::iter::FromIterator<&app_structs::Vocabulary>` is not implemented for `std::vec::Vec<app_structs::Vocabulary>` [E0277]
I don't know how to implement any FromIterator
, nor why that would be necessary. In another route in the same web app, same file I do the following, which works:
let result: Vec<String> = vocabulary_context.vocabularies.iter()
.filter(|voc| voc.metadata.identifier.as_str().contains(vocabulary_id))
.map(encode_to_string)
.collect::<Vec<String>>();
result.join("\n\n") // returning
So it seems that String
implements FromIterator
.
However, I don't get, why I cannot simple get back the Elements of the Vec
from the filter
or collect
method.
How can I filter
my Vec
and simply get the elements of the Vec<Vocabulary>
, for which the condition is true?