Rust Programming: TypeState Builder Pattern Explained

Опубликовано: 13 Октябрь 2024
на канале: Jeremy Chone
33,157
1.3k

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 ►   / jeremychone  

Previous Video on builder pattern:    • Rust Programming: The Ultimate Builde...  
GitHub source (with the other builder patterns): https://github.com/jeremychone-channe...

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" (https://www.greyblake.com/blog/builde...)

Jeremy Chone:

Twitter -   / jeremychone  
Discord On Rust -   / discord  
AWESOME-APP - https://awesomeapp.dev - Rust Templates for building awesome applications.
Patreon -   / jeremychone   - Any help is a big help (for Rust educational content)


Other popular Rust Programming videos:

Rust Builder Pattern -    • Rust Programming: The Ultimate Builde...  
Quick Start Code Layout -    • Rust - Simple Code Layout (start simp...  
AWESOME-APP Full Overview - Rust template for building Awesome Desktop Application:    • Building Awesome Desktop App with Rus...  
Tauri Quick Introduction (Desktop App wit Rust Programming):    • Rust Tauri 1.0 - Quick Intro - Rust D...  
Rust Web App tutorials series:    • Rust Web App - 1/3 - Data Access  
Rust Bevy Full Tutorial - Game Development with Rust:    • Rust Bevy Full Tutorial - Game Develo...  
Rust for Java Developers series:    • Rust for Java Developers 1/3 - Introd...  

Playlists:

Rust For Desktop App:    • JC - Rust Programming for Desktop App  
Everything Rust Programming - Tutorials, Courses, Tips, Examples:    • JC - Rust - Everything Rust Programmi...  
Rust Programming for Web Development:    • JC - Rust Programming for Web Develop...  
Rust Courses:    • Rust Course 2021 by the book | RustLang  
Rust for Java Developers:    • Rust for Java Developers  


Other notes:

Tool used to do the green lines. ScreenBrush on Mac App Store (Gromit seems to be the equivalent on Linux)
Edited with Davinci Resolve.