Skip to content

Instantly share code, notes, and snippets.

@mykhailokrainik
Forked from rust-play/playground.rs
Last active November 1, 2018 12:33
Show Gist options
  • Save mykhailokrainik/0b507521922203879ac6c6db899eb620 to your computer and use it in GitHub Desktop.
Save mykhailokrainik/0b507521922203879ac6c6db899eb620 to your computer and use it in GitHub Desktop.
Rust Generators
#![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"),
}
}
@mykhailokrainik
Copy link
Author

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:

@mykhailokrainik
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment