petgraph/
iter_utils.rs

1pub trait IterUtilsExt: Iterator {
2    /// Return the first element that maps to `Some(_)`, or None if the iterator
3    /// was exhausted.
4    fn ex_find_map<F, R>(&mut self, mut f: F) -> Option<R>
5    where
6        F: FnMut(Self::Item) -> Option<R>,
7    {
8        for elt in self {
9            if let result @ Some(_) = f(elt) {
10                return result;
11            }
12        }
13        None
14    }
15
16    /// Return the last element from the back that maps to `Some(_)`, or
17    /// None if the iterator was exhausted.
18    fn ex_rfind_map<F, R>(&mut self, mut f: F) -> Option<R>
19    where
20        F: FnMut(Self::Item) -> Option<R>,
21        Self: DoubleEndedIterator,
22    {
23        while let Some(elt) = self.next_back() {
24            if let result @ Some(_) = f(elt) {
25                return result;
26            }
27        }
28        None
29    }
30}
31
32impl<I> IterUtilsExt for I where I: Iterator {}