Pretty State Machine Patterns in Rust

(hoverbear.org)

35 points | by PaulHoule 4 hours ago

5 comments

  • gsliepen 23 minutes ago
    I don't understand why you would code these explicit state machines when you can just write normal code that is much more readable. The state machine example they start with could be written as:

      while (true) {
         wait();
         fill();
         finish();
      }
    
    I don't think the approach from the article would have any benefits like less bugs or higher performance.
    • skavi 11 minutes ago
      allows trivially mocking effects for testing
  • skavi 8 minutes ago
    i think stable coroutines [0] would be huge for rust. they would enable writing pure state machines in the form of straight line imperative code.

    currently they’re used in the implementation of async/await, but aren’t themselves exposed.

    [0]: https://doc.rust-lang.org/beta/unstable-book/language-featur...

  • gnabgib 4 hours ago
    (2016) Popular, but barely discussed at the time (62 points, 3 comments) https://news.ycombinator.com/item?id=12703623
  • locusofself 2 hours ago
    Is the title a nod to the Nine Inch Nails album "Pretty Hate Machine" ?
  • jesse__ 1 hour ago
    Honestly, I prefer the long-form first-cut that he dismisses to save 10 lines of code at the cost of using generics.
    • joshka 18 minutes ago
    • jesse__ 1 hour ago
      On a second skim it is nice that errors are surfaced at compile time, but honestly I'm not sure that's worth the complexity.
      • kadoban 54 minutes ago
        I feel like either one could be a reasonable choice, mostly depending on how complex the state machine is and how bad it is if it errors at runtime.