diff --git a/src/idiomatic/leveraging-the-type-system/typestate-pattern.md b/src/idiomatic/leveraging-the-type-system/typestate-pattern.md index e10d12e5..a67dadd2 100644 --- a/src/idiomatic/leveraging-the-type-system/typestate-pattern.md +++ b/src/idiomatic/leveraging-the-type-system/typestate-pattern.md @@ -4,8 +4,7 @@ minutes: 15 ## Typestate Pattern -The typestate pattern uses Rust’s type system to make **invalid states -unrepresentable**. +Typestate is the practice of encoding a part of the state of the value in its type, preventing incorrect or inapplicable operations from being called on the value. ```rust # use std::fmt::Write; @@ -52,10 +51,19 @@ fn main() { - The typestate pattern allows us to model state machines using Rust’s type system. In this case, the state machine is a simple serializer. -- The key idea is that each state in the process, starting a struct, writing - fields, and finishing, is represented by a different type. Transitions between +- The key idea is that at each state in the process, we can only + do the actions which are valid for that state. Transitions between states happen by consuming one value and producing another. +```bob ++------------+ serialize struct +-----------------+ +| Serializer +-------------------->| SerializeStruct |<-------+ ++------------+ +-+-----+---------+ | + ^ | | | + | finish struct | | serialize field | + +-----------------------------+ +------------------+ +``` + - In the example above: - Once we begin serializing a struct, the `Serializer` is moved into the