How to filter a vector of custom structs in Rust? : <p>I am trying to filter a <code>Vec&lt;Vocabulary&gt;</code> where <code>Vocabulary</code> is a custom <code>struct</code>, which itself contains a <code>struct</code> <code>VocabularyMetadata</code> and a <code>Vec&lt;Word&gt;</code>:</p>

<pre><code>#[derive(Serialize, Deserialize)]
pub struct Vocabulary {
    pub metadata: VocabularyMetadata,
    pub words: Vec&lt;Word&gt;
}
</code></pre>

<p>This is for handling a route in a web application, where the route looks like this: <code>/word/&lt;vocabulary_id&gt;/&lt;word_id&gt;</code>.</p>

<p>Here is my current code trying to <code>filter</code> the <code>Vec&lt;Vocabulary&gt;</code>:</p>

<pre><code>let the_vocabulary: Vec&lt;Vocabulary&gt; = vocabulary_context.vocabularies.iter()
    .filter(|voc| voc.metadata.identifier == vocabulary_id)
    .collect::&lt;Vec&lt;Vocabulary&gt;&gt;();
</code></pre>

<p>This does not work. The error I get is:</p>

<pre><code> the trait `std::iter::FromIterator&lt;&amp;app_structs::Vocabulary&gt;` is not implemented for `std::vec::Vec&lt;app_structs::Vocabulary&gt;` [E0277]
</code></pre>

<p>I don't know how to implement any <code>FromIterator</code>, nor why that would be necessary. In another route in the same web app, same file I do the following, which works:</p>

<pre><code>let result: Vec&lt;String&gt; = vocabulary_context.vocabularies.iter()
    .filter(|voc| voc.metadata.identifier.as_str().contains(vocabulary_id))
    .map(encode_to_string)
    .collect::&lt;Vec&lt;String&gt;&gt;();
    result.join("\n\n")  // returning
</code></pre>

<p>So it seems that <code>String</code> implements <code>FromIterator</code>.</p>

<p>However, I don't get, why I cannot simple get back the Elements of the <code>Vec</code> from the <code>filter</code> or <code>collect</code> method.</p>

<p>How can I <code>filter</code> my <code>Vec</code> and simply get the elements of the <code>Vec&lt;Vocabulary&gt;</code>, for which the condition is true?</p>
