Using generators
The biggest concept that is still not quite usable yet in Rust is easy async. One reason for that is a lack of compiler support for certain things, which is being worked on right now. One important part of the road to async is generators, which are implemented similarly to how they're used in C# or Python.
How to do it...
- Open the
Cargo.toml
file that has been generated earlier for you. - In the
bin
folder, create a file calledgenerator.rs
. - Add the following code, and run it with
cargo run --bin generator
:
1 #![feature(generators, generator_trait, conservative_impl_trait)] 2 3 fn main() { 4 // A closure that uses the keyword "yield" is called a generator 5 // Yielding a value "remembers" where you left off 6 // when calling .resume() on the generator 7 let mut generator = || { 8 yield 1; 9 yield 2; 10 }; 11 if let GeneratorState::Yielded(value) = generator.resume() { 12 println!("The generator yielded: {}", value); 13...