Struct std::ops::generator::Generator

Overview

A generator produced by a generator function.

Generator are functions or closures which contain the yield expressions.

Examples

use std::ops::generator::Generator;

let f = |n| {
   yield n;
   yield n + 1;
};

let g = f(10);

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: any) -> GeneratorState

Resumes the execution of this generator.

This function will resume execution of the generator or start execution if it hasn't already. This call will return back into the generator's last suspension point, resuming execution from the latest yield. The generator will continue executing until it either yields or returns, at which point this function will return.

Return value

The GeneratorState enum returned from this function indicates what state the generator is in upon returning. If the Yielded variant is returned then the generator has reached a suspension point and a value has been yielded out. Generators in this state are available for resumption at a later point.

If Complete is returned then the generator has completely finished with the value provided. It is invalid for the generator to be resumed again.

Panics

This function may panic if it is called after the Complete variant has been returned previously. While generator literals in the language are guaranteed to panic on resuming after Complete, this is not guaranteed for all implementations of the Generator.

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) -> Iter

Convert a generator into an iterator.

Examples

fn count_numbers(limit) {
   for n in 0..limit.unwrap_or(10) {
       yield n;
   }
}

assert_eq!(count_numbers(None).iter().collect::<Vec>(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
assert_eq!(count_numbers(Some(2)).iter().collect::<Vec>(), [0, 1]);

Trait Implementations

impl Clone for Generator
fn clone(value: any) -> any

Clone the specified value.

Examples

let a = 42;
let b = a;
let c = a.clone();

a += 1;

assert_eq!(a, 43);
assert_eq!(b, 42);
assert_eq!(c, 42);

Protocols

protocol INTO_ITER
for item in value { }

Construct an iterator over a generator.

Examples

fn count_numbers(limit) {
   for n in 0..limit {
       yield n;
   }
}

let result = 0;

for n in count_numbers(3) {
   result += n;
}

assert_eq!(result, 3);
protocol DEBUG_FMT
format!("{:?}", value)

Debug print this generator.

Examples

use std::ops::GeneratorState;

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

let a = generate();

println!("{a:?}");
``
protocol CLONE
let $out = clone(value)

Clone a generator.

This clones the state of the generator too, allowing it to be resumed independently.

Examples

use std::ops::GeneratorState;

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

let a = generate();

assert_eq!(a.resume(()), GeneratorState::Yielded(1));
let b = a.clone();
assert_eq!(a.resume(2), GeneratorState::Yielded(4));
assert_eq!(b.resume(3), GeneratorState::Yielded(5));

assert_eq!(a.resume(()), GeneratorState::Complete(()));
assert_eq!(b.resume(()), GeneratorState::Complete(()));
``