|
|
|
|
|
by RcouF1uZ4gsC
2273 days ago
|
|
In my experience, state machines are very nice in theory, but in practice, over time, they devolve into a mess of spaghetti code. Because they are not in a single scope, loops become the equivalent of a bunch of gotos and managing lifetimes and locks becomes a problem because you can't use scope based mechanisms such as RAII. In Rust, if you want a state machine, generators are probably the long term way to go. https://doc.rust-lang.org/nightly/unstable-book/language-fea... By using generators, the compiler will generate the state machine for you based on your code, and you can use structured loops and scope based cleanup. |
|
Generators are great for tasks that fit well but in the general case state machines are more powerful and easier to debug.
I agree that if your state machine starts evolving without control then it will be a mess (like any design).