通过迭代器搜索
Iterator::find
是一个函数,它遍历一个迭代器,并搜索第一个满足某个条件的值。如果没有值满足该条件,则返回 None
。它的签名如下:
pub trait Iterator {
// The type being iterated over.
type Item;
// `find` takes `&mut self` meaning the caller may be borrowed
// and modified, but not consumed.
fn find<P>(&mut self, predicate: P) -> Option<Self::Item> where
// `FnMut` meaning any captured variable may at most be
// modified, not consumed. `&Self::Item` states it takes
// arguments to the closure by reference.
P: FnMut(&Self::Item) -> bool;
}
Iterator::find
返回对项的引用。但如果你想要该项的索引,请使用 Iterator::position
。