std-method
Iterator::enumerate
Pair each emitted item with a zero-based usize counter. After filtering, enumeration counts the filtered sequence, not original positions. Expected stdout (not executed): [(0, "tea"), (1, "rice")] Common mistake: Assuming enumerate after filter returns original indexes.
Formal Syntax & Structure
rust syntaxrust
fn enumerate(self) -> Enumerate<Self> where Self: SizedWorking Example
Example Snippetrust
fn main() { let kept: Vec<_> = ["", "tea", "rice"].into_iter().filter(|s| !s.is_empty()).enumerate().collect(); println!("{kept:?}");}