diff --git a/src/libcore/iter/mod.rs b/src/libcore/iter/mod.rs index b6bb5f01b2d..4e1dc108cc9 100644 --- a/src/libcore/iter/mod.rs +++ b/src/libcore/iter/mod.rs @@ -327,7 +327,7 @@ pub use self::sources::{Once, once}; #[unstable(feature = "iter_once_with", issue = "57581")] pub use self::sources::{OnceWith, once_with}; #[unstable(feature = "iter_unfold", issue = "55977")] -pub use self::sources::{Unfold, unfold, Successors, successors}; +pub use self::sources::{FromFn, from_fn, Successors, successors}; #[stable(feature = "rust1", since = "1.0.0")] pub use self::traits::{FromIterator, IntoIterator, DoubleEndedIterator, Extend}; diff --git a/src/libcore/iter/sources.rs b/src/libcore/iter/sources.rs index 2590fa6023a..2b741e66170 100644 --- a/src/libcore/iter/sources.rs +++ b/src/libcore/iter/sources.rs @@ -491,24 +491,24 @@ pub fn once_with A>(gen: F) -> OnceWith { } /// Creates a new iterator where each iteration calls the provided closure -/// `F: FnMut(&mut St) -> Option`. +/// `F: FnMut() -> Option`. /// /// This allows creating a custom iterator with any behavior /// without using the more verbose syntax of creating a dedicated type /// and implementing the `Iterator` trait for it. /// -/// In addition to its captures and environment, -/// the closure is given a mutable reference to some state -/// that is preserved across iterations. -/// That state starts as the given `initial_state` value. -/// -/// Note that the `Unfold` iterator doesn’t make assumptions about the behavior of the closure, +/// Note that the `FromFn` iterator doesn’t make assumptions about the behavior of the closure, /// and therefore conservatively does not implement [`FusedIterator`], /// or override [`Iterator::size_hint`] from its default `(0, None)`. /// /// [`FusedIterator`]: trait.FusedIterator.html /// [`Iterator::size_hint`]: trait.Iterator.html#method.size_hint /// +/// The closure can use its its captures and environment +/// to track state across iterations. +/// Depending on how the iterator is used, +/// this may require specifying the `move` keyword on the closure. +/// /// # Examples /// /// Let’s re-implement the counter iterator from [module-level documentation]: @@ -517,13 +517,14 @@ pub fn once_with A>(gen: F) -> OnceWith { /// /// ``` /// #![feature(iter_unfold)] -/// let counter = std::iter::unfold(0, |count| { +/// let mut count = 0; +/// let counter = std::iter::from_fn(move || { /// // Increment our count. This is why we started at zero. -/// *count += 1; +/// count += 1; /// /// // Check to see if we've finished counting or not. -/// if *count < 6 { -/// Some(*count) +/// if count < 6 { +/// Some(count) /// } else { /// None /// } @@ -532,46 +533,38 @@ pub fn once_with A>(gen: F) -> OnceWith { /// ``` #[inline] #[unstable(feature = "iter_unfold", issue = "55977")] -pub fn unfold(initial_state: St, f: F) -> Unfold - where F: FnMut(&mut St) -> Option +pub fn from_fn(f: F) -> FromFn + where F: FnMut() -> Option { - Unfold { - state: initial_state, - f, - } + FromFn(f) } -/// An iterator where each iteration calls the provided closure `F: FnMut(&mut St) -> Option`. +/// An iterator where each iteration calls the provided closure `F: FnMut() -> Option`. /// -/// This `struct` is created by the [`unfold`] function. +/// This `struct` is created by the [`iter::from_fn`] function. /// See its documentation for more. /// -/// [`unfold`]: fn.unfold.html +/// [`iter::from_fn`]: fn.from_fn.html #[derive(Clone)] #[unstable(feature = "iter_unfold", issue = "55977")] -pub struct Unfold { - state: St, - f: F, -} +pub struct FromFn(F); #[unstable(feature = "iter_unfold", issue = "55977")] -impl Iterator for Unfold - where F: FnMut(&mut St) -> Option +impl Iterator for FromFn + where F: FnMut() -> Option { type Item = T; #[inline] fn next(&mut self) -> Option { - (self.f)(&mut self.state) + (self.0)() } } #[unstable(feature = "iter_unfold", issue = "55977")] -impl fmt::Debug for Unfold { +impl fmt::Debug for FromFn { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("Unfold") - .field("state", &self.state) - .finish() + f.debug_struct("FromFn").finish() } }