diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs
index 8e2c887a161e..d94a508b5b28 100644
--- a/library/core/src/iter/traits/iterator.rs
+++ b/library/core/src/iter/traits/iterator.rs
@@ -3094,16 +3094,23 @@ fn position
(&mut self, predicate: P) -> Option
P: FnMut(Self::Item) -> bool,
{
#[inline]
- fn check(
- mut predicate: impl FnMut(T) -> bool,
- ) -> impl FnMut(usize, T) -> ControlFlow {
+ fn check<'a, T>(
+ mut predicate: impl FnMut(T) -> bool + 'a,
+ acc: &'a mut usize,
+ ) -> impl FnMut((), T) -> ControlFlow + 'a {
#[rustc_inherit_overflow_checks]
- move |i, x| {
- if predicate(x) { ControlFlow::Break(i) } else { ControlFlow::Continue(i + 1) }
+ move |_, x| {
+ if predicate(x) {
+ ControlFlow::Break(*acc)
+ } else {
+ *acc += 1;
+ ControlFlow::Continue(())
+ }
}
}
- self.try_fold(0, check(predicate)).break_value()
+ let mut acc = 0;
+ self.try_fold((), check(predicate, &mut acc)).break_value()
}
/// Searches for an element in an iterator from the right, returning its