mirror of
https://github.com/rust-lang/rust.git
synced 2025-09-30 00:03:49 +00:00

This adds an `iter!` macro that can be used to create movable generators. This also adds a yield_expr feature so the `yield` keyword can be used within iter! macro bodies. This was needed because several unstable features each need `yield` expressions, so this allows us to stabilize them separately from any individual feature. Co-authored-by: Oli Scherer <github35764891676564198441@oli-obk.de> Co-authored-by: Jieyou Xu <jieyouxu@outlook.com> Co-authored-by: Travis Cross <tc@traviscross.com>
28 lines
607 B
Rust
28 lines
607 B
Rust
//@ run-pass
|
|
|
|
#![feature(iter_macro, yield_expr)]
|
|
|
|
use std::iter::iter;
|
|
|
|
fn main() {
|
|
let i = {
|
|
let s = String::new();
|
|
iter! { move || {
|
|
yield s.len();
|
|
for x in 5..10 {
|
|
yield x * 2;
|
|
}
|
|
}}
|
|
};
|
|
let mut i = i();
|
|
assert_eq!(i.next(), Some(0));
|
|
assert_eq!(i.next(), Some(10));
|
|
assert_eq!(i.next(), Some(12));
|
|
assert_eq!(i.next(), Some(14));
|
|
assert_eq!(i.next(), Some(16));
|
|
assert_eq!(i.next(), Some(18));
|
|
assert_eq!(i.next(), None);
|
|
assert_eq!(i.next(), None);
|
|
assert_eq!(i.next(), None);
|
|
}
|