Hacker News new | ask | show | jobs
by TheCoelacanth 2501 days 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-featur...

1 comments

Just 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.