std-method

Option::filter

Keep a Some only if its borrowed payload satisfies the predicate. Otherwise return None. The original Option is consumed, although the predicate receives a shared reference. Expected stdout (not executed): None Common mistake: Expecting the predicate to receive owned T.

Formal Syntax & Structure

rust syntaxrust
fn filter<P>(self, predicate: P) -> Option<T> where P: FnOnce(&T) -> bool

Working Example

Example Snippetrust
fn main() {    let count = Some(3);    println!("{:?}", count.filter(|n| *n >= 5));}
Rust Reference