File tree 4 files changed +58
-0
lines changed
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};
420
420
issue = "42168"
421
421
) ]
422
422
pub use self :: range:: Step ;
423
+ #[ unstable( feature = "iter_macro" , issue = "none" , reason = "generators are unstable" ) ]
424
+ pub use self :: sources:: iter;
423
425
#[ stable( feature = "iter_empty" , since = "1.2.0" ) ]
424
426
pub use self :: sources:: { Empty , empty} ;
425
427
#[ unstable(
Original file line number Diff line number Diff line change 1
1
mod empty;
2
2
mod from_coroutine;
3
3
mod from_fn;
4
+ mod generator;
4
5
mod once;
5
6
mod once_with;
6
7
mod repeat;
@@ -18,6 +19,8 @@ pub use self::empty::{Empty, empty};
18
19
pub use self :: from_coroutine:: { FromCoroutine , from_coroutine} ;
19
20
#[ stable( feature = "iter_from_fn" , since = "1.34.0" ) ]
20
21
pub use self :: from_fn:: { FromFn , from_fn} ;
22
+ #[ unstable( feature = "iter_macro" , issue = "none" , reason = "generators are unstable" ) ]
23
+ pub use self :: generator:: iter;
21
24
#[ stable( feature = "iter_once" , since = "1.2.0" ) ]
22
25
pub use self :: once:: { Once , once} ;
23
26
#[ 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::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