File tree Expand file tree Collapse file tree 4 files changed +58
-0
lines changed Expand file tree Collapse file tree 4 files changed +58
-0
lines changed Original file line number Diff line number Diff line change @@ -420,6 +420,8 @@ pub use self::adapters::{Intersperse, IntersperseWith};
420420 issue = "42168"
421421) ]
422422pub use self :: range:: Step ;
423+ #[ unstable( feature = "iter_macro" , issue = "none" , reason = "generators are unstable" ) ]
424+ pub use self :: sources:: iter;
423425#[ stable( feature = "iter_empty" , since = "1.2.0" ) ]
424426pub use self :: sources:: { Empty , empty} ;
425427#[ unstable(
Original file line number Diff line number Diff line change 11mod empty;
22mod from_coroutine;
33mod from_fn;
4+ mod generator;
45mod once;
56mod once_with;
67mod repeat;
@@ -18,6 +19,8 @@ pub use self::empty::{Empty, empty};
1819pub use self :: from_coroutine:: { FromCoroutine , from_coroutine} ;
1920#[ stable( feature = "iter_from_fn" , since = "1.34.0" ) ]
2021pub use self :: from_fn:: { FromFn , from_fn} ;
22+ #[ unstable( feature = "iter_macro" , issue = "none" , reason = "generators are unstable" ) ]
23+ pub use self :: generator:: iter;
2124#[ stable( feature = "iter_once" , since = "1.2.0" ) ]
2225pub use self :: once:: { Once , once} ;
2326#[ stable( feature = "iter_once_with" , since = "1.43.0" ) ]
Original file line number Diff line number Diff line change 1+ /// Creates a new closure that returns an iterator where each iteration steps the given
2+ /// generator to the next `yield` statement.
3+ ///
4+ /// Similar to [`iter::from_fn`], but allows arbitrary control flow.
5+ ///
6+ /// [`iter::from_fn`]: crate::iter::from_fn
7+ ///
8+ /// # Examples
9+ ///
10+ /// ```
11+ /// #![feature(iter_macro, coroutines)]
12+ ///
13+ /// let it = std::iter!{
14+ /// yield 1;
15+ /// yield 2;
16+ /// yield 3;
17+ /// };
18+ /// let v: Vec<_> = it.collect();
19+ /// assert_eq!(v, [1, 2, 3]);
20+ /// ```
21+ #[ unstable( feature = "iter_macro" , issue = "none" , reason = "generators are unstable" ) ]
22+ #[ allow_internal_unstable( coroutines, iter_from_coroutine) ]
23+ pub macro iter( $( $t: tt) * ) {
24+ || $crate:: iter:: from_coroutine ( #[ coroutine] || { $( $t) * } )
25+ }
Original file line number Diff line number Diff line change 1+ //@ run-pass
2+
3+ #![ feature( iter_macro) ]
4+ // FIXME(iter_macro): make `yield` within it legal
5+ #![ feature( coroutines) ]
6+
7+ use std:: iter:: iter;
8+
9+ fn main ( ) {
10+ let i = iter ! {
11+ yield 0 ;
12+ for x in 5 ..10 {
13+ yield x * 2 ;
14+ }
15+ } ;
16+ let mut i = i ( ) ;
17+ assert_eq ! ( i. next( ) , Some ( 0 ) ) ;
18+ assert_eq ! ( i. next( ) , Some ( 10 ) ) ;
19+ assert_eq ! ( i. next( ) , Some ( 12 ) ) ;
20+ assert_eq ! ( i. next( ) , Some ( 14 ) ) ;
21+ assert_eq ! ( i. next( ) , Some ( 16 ) ) ;
22+ assert_eq ! ( i. next( ) , Some ( 18 ) ) ;
23+ assert_eq ! ( i. next( ) , None ) ;
24+ // FIXME(iter_macro): desugar to `gen` instead of coroutines,
25+ // as the latter panic after resuming iteration.
26+ //assert_eq!(i.next(), None);
27+ //assert_eq!(i.next(), None);
28+ }
You can’t perform that action at this time.
0 commit comments