#![feature(gen_blocks)]
gen fngfn() ->i32 {
foriin1..=10 {
yield i;
}
}
fngblock() ->implIterator<Item = i32> {
gen {
foriin1..=10 {
yield i;
}
}
}
fnmain() {
foriingfn() {
println!("{i} from gfn()");
}
foriingblock() {
println!("{i} from gblock()");
}
}
Note that the block-in-fn version works better at this moment (from a developer’s PoV) because rust-analyzer currently treats gfn() as an i32 value. But the block-in-fn pattern works perfectly already.
In case the wording tripped anyone, generators (blocks and functions) have been available for a while as an unstable feature.
This works (playground):
#![feature(gen_blocks)] gen fn gfn() -> i32 { for i in 1..=10 { yield i; } } fn gblock() -> impl Iterator<Item = i32> { gen { for i in 1..=10 { yield i; } } } fn main() { for i in gfn() { println!("{i} from gfn()"); } for i in gblock() { println!("{i} from gblock()"); } }
Note that the block-in-fn version works better at this moment (from a developer’s PoV) because
rust-analyzer
currently treatsgfn()
as an i32 value. But the block-in-fn pattern works perfectly already.Amazing! Thanks for that. I didn’t know this was actually available to play with.