Skip to content
0:00 / 0:00

#typestate-pattern#builder-pattern#phantomdata

Rust Programming: TypeState Builder Pattern Explained

Jeremy Chone

33K subscribers • 3 videos on Hallway

YouTube
  • Recommended by 4 people: 4 in engineering communities.
  • A classic. People kept recommending it from 2023 to 2026.
  • Presented by Jeremy Chone
  • Linked in the descriptionjeremychone-channel/rust-builder

Intermediate. Assumes working knowledge.

45K views3 years ago4 recommendations

Programming languagesSoftware designRustDesign patternsSoftware architecture

Jeremy Chone explains how to implement the typestate builder pattern in Rust using PhantomData to enforce compile-time state constraints.

Rust Programming Tutorial for the TypeState builder pattern. We will also learn how to use PhantomData as one of the states. Help support this channel ► patreon.com/jeremychone Previous Video on builder pattern: youtube.com/watch?v=Z_3WOSiYYFY&list=PL7r-PXl6ZPcCIOFaL7nVHX… GitHub source (with the other builder patterns): github.com/jeremychone-channel/rust-builder Some Notes: 1) PhantomData are erased by the compiler, meaning they do not exist at "runtime" but allow code to use them to leverage the compiler's type system. 2) As commented by 'Harold Ruiter,' the 'build()' method has become infallible, meaning it cannot fail, and therefore, it can just return 'Request', rather than Result of Request. 3) The TypeState pattern does not have to be used only with the builder pattern. 4) As mentioned in the video, generics increase the binary size and compile time. While, more often than not, generics' value outweighs those costs, there is still a point of diminishing return if generics are overused. 5) To follow the #4 point, in this tutorial, we showed the multi states pattern with URL and Method, but designing a builder with too many states, for each property, for example, would be overkill and have counter-productive side effects. 6) In theory, the pure definition of the TypeState pattern assumes the same type is returned, which is technically not feasible in Rust, as Generic creates a different type. However, from a practical Rust perspective, this is a good representation of the TypeState pattern. Thanks to Sergey Potapov's great blog post on "Builder with typestate in Rust" (greyblake.com/blog/builder-with-typestate-in-rust/) Jeremy Chone: - Twitter - twitter.com/jeremychone - Discord On Rust - discord.gg/W2besKCzjx - AWESOME-APP - awesomeapp.dev - Rust Templates for building awesome applications. - Patreon - patreon.com/jeremychone - Any help is a big help (for Rust educational content) Other popular Rust Programming videos: - Rust Builder Pattern - youtube.com/watch?v=Z_3WOSiYYFY&list=PL7r-PXl6ZPcCIOFaL7nVHX… - Quick Start Code Layout - youtube.com/watch?v=oxx7MmN4Ib0&list=PL7r-PXl6ZPcCIOFaL7nVHX… - AWESOME-APP Full Overview - Rust template for building Awesome Desktop Application: youtube.com/watch?v=BY_ZjPGqJJk&list=PL7r-PXl6ZPcCIOFaL7nVHX… - Tauri Quick Introduction (Desktop App wit Rust Programming): youtube.com/watch?v=kRoGYgAuZQE&list=PL7r-PXl6ZPcCIOFaL7nVHX… - Rust Web App tutorials series: youtube.com/watch?v=VIig9IcQ-w8&list=PL7r-PXl6ZPcCIOFaL7nVHX… - Rust Bevy Full Tutorial - Game Development with Rust: youtube.com/watch?v=j7qHwb7geIM&list=PL7r-PXl6ZPcCIOFaL7nVHX… - Rust for Java Developers series: youtube.com/watch?v=iFdh4sPC5Tg&list=PL7r-PXl6ZPcD63D

Why it's here

4 people recommend this

What people say. Recommended as an excellent explanation of the typestate pattern and compile-time state machines in Rust.

u/Known_Cod8398in r/rust • Recommends

On “🦀 Statum: Zero-Boilerplate Compile-Time State Machines in Rust”

I think this is a good place to start!

u/eboodyin r/rust • Recommends

On “Is Rust suitable for me?”

To piggy back off of this comment: After reading the rust book I would do Rustlings and any rustlings adjacent projects as needed Then, id watch Jeremy Chone's Axum Course on YouTube. I consume a lot of Rust content on YouTube and I haven't come across anything on his level of quality yet. He goes beyond the framework and talks about best practices both in terms of Rust idioms but also just about principles for building a proper web-server. His videos on the Builder Pattern and Typestate Builder Pattern are also top tier!

u/phazer99in r/rust • Recommends

On “Common idioms”

I think the most unique thing about Rust is the borrowing and ownership rules. They entail some design patterns not commonly found in other languages. Here some resources to study: * Rust book ownership chapter * Understanding Ownership in Rust * Rust TypeState Builder Pattern * Crust of Rust: Smart Pointers and Interior Mutability

u/phazer99in r/rust • Recommends

On “Resources For Rust's Type System”

Some examples: * Make invalid states unrepresentable * Type state builder pattern But some of the most interesting and useful patterns are found in the combination of the Send and Sync traits with the aliasing and ownership rules. For example, the implementations of channels, Mutex, rtrb (a SPSC ring buffer) etc. which contains some `unsafe` code internally, but with clever design patterns expose an API that is statically guaranteed to be safe for multi-thread usage. Libraries like this would be much more error prone to implement and use in other languages.

u/star_sky_musicin r/rust • Recommends

On “it is possible to (exhaustively) force the usage of specific destructors”

If you want to force the usage of a specific method, then use the Type State Builder pattern. Along with it use the concept of Sealing the constructor or destructor using Phantom Data. link to this video

u/Known_Cod8398in r/rust • Recommends

On “🦀 Statum: Zero-Boilerplate Compile-Time State Machines in Rust”

you should watch this video! I think it would clear up a lot of the confusion about what a typestate builder is