-
-
Save mykhailokrainik/0b507521922203879ac6c6db899eb620 to your computer and use it in GitHub Desktop.
Rust Generators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(unused)] | |
#![feature(generators, generator_trait)] | |
use std::ops::{Generator, GeneratorState}; | |
fn main() { | |
let mut generator = || { | |
yield 1; | |
return "foo" | |
}; | |
match unsafe { generator.resume() } { | |
GeneratorState::Yielded(one) => { println!("{}", one) } | |
_ => panic!("unexpected return from resume"), | |
} | |
match unsafe { generator.resume() } { | |
GeneratorState::Complete(returned) => { println!("{}", returned)} | |
_ => panic!("unexpected return from resume"), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Generators, also commonly referred to as coroutines, are currently an experimental language feature in Rust. Added in RFC 2033 generators are currently intended to primarily provide a building block for async/await syntax but will likely extend to also providing an ergonomic definition for iterators and other primitives.
The syntax and semantics for generators is unstable and will require a further RFC for stabilization. At this time, though, the syntax is closure-like: