petgraph/
iter_utils.rs

1
2pub trait IterUtilsExt : Iterator {
3    /// Return the first element that maps to `Some(_)`, or None if the iterator
4    /// was exhausted.
5    fn ex_find_map<F, R>(&mut self, mut f: F) -> Option<R>
6        where 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 F: FnMut(Self::Item) -> Option<R>,
20              Self: DoubleEndedIterator,
21    {
22        while let Some(elt) = self.next_back() {
23            if let result @ Some(_) = f(elt) {
24                return result;
25            }
26        }
27        None
28    }
29}
30
31impl<I> IterUtilsExt for I where I: Iterator { }