Struct ::std::ops::Generator

Overview

The return value of a function producing a generator.

Functions which contain the yield keyword produces generators.

Examples

use std::ops::Generator;

fn generate() {
   yield 1;
   yield 2;
}

let g = generate();
assert!(g is Generator)

Methods

fn next(self) -> Option

Advance a generator producing the next value yielded.

Unlike [Generator::resume], this can only consume the yielded values.

Examples

use std::ops::{Generator, GeneratorState};

fn generate() {
   yield 1;
   yield 2;
}

let g = generate();

assert_eq!(g.next(), Some(1));
assert_eq!(g.next(), Some(2));
assert_eq!(g.next(), None);
``
fn resume(self, value) -> GeneratorState

Advance a generator producing the next GeneratorState.

Examples

use std::ops::{Generator, GeneratorState};

fn generate() {
   let n = yield 1;
   yield 2 + n;
}

let g = generate();

assert_eq!(g.resume(()), GeneratorState::Yielded(1));
assert_eq!(g.resume(1), GeneratorState::Yielded(3));
assert_eq!(g.resume(()), GeneratorState::Complete(()));
``
fn iter(self) -> Iterator

Protocols

protocol into_iter
for item in value { }

Allows the value to be converted into an iterator in a for-loop.