@@ -491,24 +491,24 @@ pub fn once_with<A, F: FnOnce() -> A>(gen: F) -> OnceWith<F> {
491
491
}
492
492
493
493
/// Creates a new iterator where each iteration calls the provided closure
494
- /// `F: FnMut(&mut St ) -> Option<T>`.
494
+ /// `F: FnMut() -> Option<T>`.
495
495
///
496
496
/// This allows creating a custom iterator with any behavior
497
497
/// without using the more verbose syntax of creating a dedicated type
498
498
/// and implementing the `Iterator` trait for it.
499
499
///
500
- /// In addition to its captures and environment,
501
- /// the closure is given a mutable reference to some state
502
- /// that is preserved across iterations.
503
- /// That state starts as the given `initial_state` value.
504
- ///
505
- /// Note that the `Unfold` iterator doesn’t make assumptions about the behavior of the closure,
500
+ /// Note that the `FromFn` iterator doesn’t make assumptions about the behavior of the closure,
506
501
/// and therefore conservatively does not implement [`FusedIterator`],
507
502
/// or override [`Iterator::size_hint`] from its default `(0, None)`.
508
503
///
509
504
/// [`FusedIterator`]: trait.FusedIterator.html
510
505
/// [`Iterator::size_hint`]: trait.Iterator.html#method.size_hint
511
506
///
507
+ /// The closure can use its its captures and environment
508
+ /// to track state across iterations.
509
+ /// Depending on how the iterator is used,
510
+ /// this may require specifying the `move` keyword on the closure.
511
+ ///
512
512
/// # Examples
513
513
///
514
514
/// Let’s re-implement the counter iterator from [module-level documentation]:
@@ -517,13 +517,14 @@ pub fn once_with<A, F: FnOnce() -> A>(gen: F) -> OnceWith<F> {
517
517
///
518
518
/// ```
519
519
/// #![feature(iter_unfold)]
520
- /// let counter = std::iter::unfold(0, |count| {
520
+ /// let mut count = 0;
521
+ /// let counter = std::iter::from_fn(move || {
521
522
/// // Increment our count. This is why we started at zero.
522
- /// * count += 1;
523
+ /// count += 1;
523
524
///
524
525
/// // Check to see if we've finished counting or not.
525
- /// if * count < 6 {
526
- /// Some(* count)
526
+ /// if count < 6 {
527
+ /// Some(count)
527
528
/// } else {
528
529
/// None
529
530
/// }
@@ -532,46 +533,38 @@ pub fn once_with<A, F: FnOnce() -> A>(gen: F) -> OnceWith<F> {
532
533
/// ```
533
534
#[ inline]
534
535
#[ unstable( feature = "iter_unfold" , issue = "55977" ) ]
535
- pub fn unfold < St , T , F > ( initial_state : St , f : F ) -> Unfold < St , F >
536
- where F : FnMut ( & mut St ) -> Option < T >
536
+ pub fn from_fn < T , F > ( f : F ) -> FromFn < F >
537
+ where F : FnMut ( ) -> Option < T >
537
538
{
538
- Unfold {
539
- state : initial_state,
540
- f,
541
- }
539
+ FromFn ( f)
542
540
}
543
541
544
- /// An iterator where each iteration calls the provided closure `F: FnMut(&mut St ) -> Option<T>`.
542
+ /// An iterator where each iteration calls the provided closure `F: FnMut() -> Option<T>`.
545
543
///
546
- /// This `struct` is created by the [`unfold `] function.
544
+ /// This `struct` is created by the [`iter::from_fn `] function.
547
545
/// See its documentation for more.
548
546
///
549
- /// [`unfold `]: fn.unfold .html
547
+ /// [`iter::from_fn `]: fn.from_fn .html
550
548
#[ derive( Clone ) ]
551
549
#[ unstable( feature = "iter_unfold" , issue = "55977" ) ]
552
- pub struct Unfold < St , F > {
553
- state : St ,
554
- f : F ,
555
- }
550
+ pub struct FromFn < F > ( F ) ;
556
551
557
552
#[ unstable( feature = "iter_unfold" , issue = "55977" ) ]
558
- impl < St , T , F > Iterator for Unfold < St , F >
559
- where F : FnMut ( & mut St ) -> Option < T >
553
+ impl < T , F > Iterator for FromFn < F >
554
+ where F : FnMut ( ) -> Option < T >
560
555
{
561
556
type Item = T ;
562
557
563
558
#[ inline]
564
559
fn next ( & mut self ) -> Option < Self :: Item > {
565
- ( self . f ) ( & mut self . state )
560
+ ( self . 0 ) ( )
566
561
}
567
562
}
568
563
569
564
#[ unstable( feature = "iter_unfold" , issue = "55977" ) ]
570
- impl < St : fmt :: Debug , F > fmt:: Debug for Unfold < St , F > {
565
+ impl < F > fmt:: Debug for FromFn < F > {
571
566
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
572
- f. debug_struct ( "Unfold" )
573
- . field ( "state" , & self . state )
574
- . finish ( )
567
+ f. debug_struct ( "FromFn" ) . finish ( )
575
568
}
576
569
}
577
570
0 commit comments