3 ms·
It gets converted into a state machine. || { yield 2; yield 3; yield 5; } will get converted to a struct that implements the Generator trait[1
by TheCoelacanth 7y ago
It gets converted into a state machine.
|| {
yield 2;
yield 3;
yield 5;
}
will get converted to a struct that implements the Generator trait[1] with a resume method something like
fn resume(self: Pin<&mut Self>) -> GeneratorState<i32, ()> {
match self.next_state {
0 => {
self.next_state = 1;
Yielded(2)
},
1 => {
self.next_state = 2;
Yielded(3)
},
2 => {
self.next_state = 3;
Yielded(5)
},
_ => Complete(())
}
}
Local variables become fields in the struct and if you have more complex control flow, the state could end up jumping around instead of just increasing by one each time. It's nothing that you couldn't write by hand, but it would be very tedious to do so.
[1] https://doc.rust-lang.org/beta/unstable-book/language-features/generators.html https://doc.rust-lang.org/beta/unstable-book/language-featur...
- kzrdude 7y agoJust like for async, borrow across yield points here are a special feature that you couldn't implement in Rust as an open coded state machine, you'd have to find a workaround.