std-method

Iterator::count

Return how many items the receiver yields before stopping. The receiver is consumed; a count after filter measures matching items rather than the original length. Expected stdout (not executed): 3 Common mistake: Counting an unbounded iterator without a stopping condition.

Formal Syntax & Structure

rust syntaxrust
fn count(self) -> usize where Self: Sized

Working Example

Example Snippetrust
fn main() {    let active = [true, false, true, true].into_iter().filter(|flag| *flag).count();    println!("{active}");}
Rust Reference