///
/// let iter = a.iter();
///
- /// let sum: i32 = iter.take(5).fold(0, |acc, i| acc + i );
+ /// let sum: i32 = iter.take(5).fold(0, |acc, i| acc + i);
///
/// assert_eq!(sum, 6);
///
/// let mut iter = a.iter();
///
/// // instead, we add in a .by_ref()
- /// let sum: i32 = iter.by_ref().take(2).fold(0, |acc, i| acc + i );
+ /// let sum: i32 = iter.by_ref().take(2).fold(0, |acc, i| acc + i);
///
/// assert_eq!(sum, 3);
///
}
/// Applies function to the elements of iterator and returns
- /// the first non-none result or the first error.
+ /// the first true result or the first error.
///
/// # Examples
///
/// ```
#[inline]
#[unstable(feature = "try_find", reason = "new API", issue = "63178")]
- fn try_find<F, E, R>(&mut self, mut f: F) -> Result<Option<Self::Item>, E>
+ fn try_find<F, R>(&mut self, f: F) -> Result<Option<Self::Item>, R::Error>
where
Self: Sized,
F: FnMut(&Self::Item) -> R,
- R: Try<Ok = bool, Error = E>,
+ R: Try<Ok = bool>,
{
- self.try_fold((), move |(), x| match f(&x).into_result() {
- Ok(false) => LoopState::Continue(()),
- Ok(true) => LoopState::Break(Ok(x)),
- Err(x) => LoopState::Break(Err(x)),
- })
- .break_value()
- .transpose()
+ #[inline]
+ fn check<F, T, R>(mut f: F) -> impl FnMut((), T) -> LoopState<(), Result<T, R::Error>>
+ where
+ F: FnMut(&T) -> R,
+ R: Try<Ok = bool>,
+ {
+ move |(), x| match f(&x).into_result() {
+ Ok(false) => LoopState::Continue(()),
+ Ok(true) => LoopState::Break(Ok(x)),
+ Err(x) => LoopState::Break(Err(x)),
+ }
+ }
+
+ self.try_fold((), check(f)).break_value().transpose()
}
/// Searches for an element in an iterator, returning its index.
/// ```
/// let a = [1, 2, 3];
///
- /// let v_cloned: Vec<_> = a.iter().copied().collect();
+ /// let v_copied: Vec<_> = a.iter().copied().collect();
///
/// // copied is the same as .map(|&x| x)
/// let v_map: Vec<_> = a.iter().map(|&x| x).collect();
///
- /// assert_eq!(v_cloned, vec![1, 2, 3]);
+ /// assert_eq!(v_copied, vec![1, 2, 3]);
/// assert_eq!(v_map, vec![1, 2, 3]);
/// ```
#[stable(feature = "iter_copied", since = "1.36.0")]