u/Known_Cod8398in r/rust • Recommends
On “🦀 Statum: Zero-Boilerplate Compile-Time State Machines in Rust”
#typestate-pattern#builder-pattern#phantomdata
33K subscribers • 3 videos on Hallway
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-PXl6ZPcD63DWhat 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”
u/eboodyin r/rust • Recommends
On “Is Rust suitable for me?”
u/phazer99in r/rust • Recommends
On “Common idioms”
u/phazer99in r/rust • Recommends
On “Resources For Rust's Type System”
u/star_sky_musicin r/rust • Recommends
On “it is possible to (exhaustively) force the usage of specific destructors”
u/Known_Cod8398in r/rust • Recommends
On “🦀 Statum: Zero-Boilerplate Compile-Time State Machines in Rust”