std-method

Iterator::position — locate an index

Return the zero-based offset of the first matching item relative to the current iterator position. The search consumes through the match. Expected stdout (not executed): Some(1) Common mistake: Treating the offset as the original collection index after earlier consumption.

Formal Syntax & Structure

rust syntaxrust
fn position<P>(&mut self, predicate: P) -> Option<usize> where Self: Sized, P: FnMut(Self::Item) -> bool

Working Example

Example Snippetrust
fn main() {    let mut names = ["Ada", "Lin", "Sam"].into_iter();    println!("{:?}", names.position(|s| s == "Lin"));}
Rust Reference