std-method
Iterator::filter
Keep items whose predicate returns true. The predicate receives a reference to Item; if Item is itself a reference, it receives another reference layer. Expected stdout (not executed): [3, 8] Common mistake: Assuming filter can change the item type.
Formal Syntax & Structure
rust syntaxrust
fn filter<P: FnMut(&Self::Item) -> bool>(self, predicate: P) -> Filter<Self, P> where Self: SizedWorking Example
Example Snippetrust
fn main() { let available: Vec<_> = [0, 3, 0, 8].into_iter().filter(|n| *n > 0).collect(); println!("{available:?}");}