Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Coroutines are another interesting way of expressing state machines. They can make certain classes of state machines easier to read because the flow reads like a normal function. So far I've only used the technique once in Python using generators. There were some ergonomic aspects that were less than ideal but serve as one example of why I look forward to generators being added to Rust.


Async/await in Rust compiles down to a state machine (and to my understanding, uses generators under the hood):

https://rust-lang.github.io/async-book/01_getting_started/04...


Generators are currently unstable, but the crate genawaiter[1] allows you to use them on stable.

We have used that crate at work to replace a very scary and verbose state machine (which was generating a lot more code via procedural macros) with a much more succinct version.

[1]: https://docs.rs/genawaiter/0.2.2/genawaiter/




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: