Functional State Machines in Rust: Typestate and Newtype Patterns (Experience Report) | Proceedings of the 4th ACM SIGPLAN International Workshop on Functional Software Architecture

· ACM Conferences

3 min read Original article ↗

Abstract

Abstract

We investigate the effects on code quality of the design patterns typestate and newtype in the language Rust, by conducting three case studies from production code, evaluated through expert interviews, static code analysis and benchmarks. The findings indicate that: (1) Typestate improves code faultlessness and testability, but comes at the cost of more boilerplate code and can degrade readability. Code with extensive branching logic and many invariants likely benefits most from the pattern. (2) Newtype combined with the "Parse, don't validate" principle improves code quality at a low cost and prevents invalid states at runtime.

AI Summary

AI-Generated Summary (Experimental)

This summary was generated using automated tools and was not authored or reviewed by the article's author(s). It is provided to support discovery, help readers assess relevance, and assist readers from adjacent research areas in understanding the work. It is intended to complement the author-supplied abstract, which remains the primary summary of the paper. The full article remains the authoritative version of record. Click here to learn more.

Click here to comment on the accuracy, clarity, and usefulness of this summary. Doing so will help inform refinements and future regenerated versions.

To view this AI-generated plain language summary, you must have Premium access.

Formats available

You can view the full content in the following formats:

References

[1]

2023. ISO/IEC 25010:2023(En), Systems and Software Engineering — Systems and Software Quality Requirements and Evaluation (SQuaRE) — Product Quality Model. International Standard ISO/IEC 25010:2023. ISO/IEC Central Secretariat, Geneva, Switzerland.

[2]

Gary Bernhardt. 2012. Functional Core, Imperative Shell. https://www.destroyallsoftware.com/screencasts/catalog/functional-core-imperative-shell

[3]

G. Ann Campbell. 2018. Cognitive Complexity: An Overview and Evaluation. In Proceedings of the 2018 International Conference on Technical Debt. ACM, Gothenburg Sweden, 57–58.

[4]

Will Crichton. 2023. Typed Design Patterns for the Functional Era. In Proceedings of the 1st ACM SIGPLAN International Workshop on Functional Software Architecture. ACM, Seattle WA USA, 40–48.

[5]

José Duarte and António Ravara. 2022. Taming Stateful Computations in Rust with Typestates. Journal of Computer Languages 72 (Oct. 2022), 101154.

[6]

Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. 1995. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley Longman Publishing Co., Inc., USA.

[7]

Gao Hao, Haytham Hijazi, Júlio Medeiros, João Durães, Chan Tong Lam, Paulo De Carvalho, and Henrique Madeira. 2026. Complementarity in Software Code Complexity Metrics. Journal of Systems and Software 232 (Feb. 2026), 112679.

[8]

Leon Heuer. 2026. Benefits of Applying Software Design Patterns to Backend Rust Applications. Bachelor’s Thesis. Nordakademie, Elmshorn. arXiv:2607.02744 [cs.SE]

[9]

Foutse Khomh and Yann-Gael Gueheneuc. 2008. Do Design Patterns Impact Software Quality Positively?. In 2008 12th European Conference on Software Maintenance and Reengineering. 274–278.

[10]

Alexis King. 2019. Parse, Don’t Validate. https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/

[11]

T.J. McCabe. 1976. A Complexity Measure. IEEE Transactions on Software Engineering SE-2, 4 (Dec. 1976), 308–320.

[12]

Norman Peitek, Sven Apel, Chris Parnin, André Brechmann, and Janet Siegmund. 2021. Program Comprehension and Code Complexity Metrics: An fMRI Study. In 2021 IEEE/ACM 43rd International Conference on Software Engineering (ICSE). 524–536.

[13]

Boqin Qin, Yilun Chen, Zeming Yu, Linhai Song, and Yiying Zhang. 2020. Understanding Memory and Thread Safety Practices and Issues in Real-World Rust Programs. In Proceedings of the 41st ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI 2020). Association for Computing Machinery, New York, NY, USA, 763–779.

[14]

Mohammad Robati Shirzad and Patrick Lam. 2024. A Study of Common Bug Fix Patterns in Rust. Empirical Software Engineering 29, 2 (Feb. 2024), 44.

[15]

Kurt D. Welker, Paul W. Oman, and Gerald G. Atkinson. 1997. Development and Application of an Automated Source Code Maintainability Index. Journal of Software Maintenance: Research and Practice 9, 3 (1997), 127–159.