from the src of core library, there is this function, but its definition is nowhere to be found. any idea?
iterator.rs
fn find<P>(&mut self, mut predicate: P) -> Option<Self::Item> where
Self: Sized,
P: FnMut(&Self::Item) -> bool,
{
self.try_for_each(move |x| {
if predicate(&x) { LoopState::Break(x) }
else { LoopState::Continue(()) }
}).break_value()
}
}
}
#[inline]
fn from_error(v: Self::Error) -> Self { LoopState::Break(v) }
#[inline]
fn from_ok(v: Self::Ok) -> Self { LoopState::Continue(v) }
}
impl<C, B> LoopState<C, B> {
#[inline]
fn break_value(self) -> Option<B> {
match self {
LoopState::Continue(..) => None,
LoopState::Break(x) => Some(x),
}
}
}
impl<R: Try> LoopState<R::Ok, R> {
#[inline]
fn from_try(r: R) -> Self {
Found by this search: https://github.com/rust-lang/rust/search?q=LoopState&unscoped_q=LoopState
Ah! thanks! that’s when google is not very helping in this case!
FWIW, IntelliJ also works awesome for these kind of searches:
Note that that code is equivalent to just
fn find<P>(&mut self, mut predicate: P) -> Option<Self::Item> where
Self: Sized,
P: FnMut(&Self::Item) -> bool,
{
self.try_for_each(move |x| {
if predicate(&x) { Err(x) }
else { Ok(()) }
}).err()
}
I just got myself super confused in the more complicated cases using Err
in the successful path, so ended up adding the extra isomorphic-to-Result enum to try to clarify the intent of the code.