Rust Programming: The Ultimate Builder Pattern Tutorial

Rust Programming: The Ultimate Builder Pattern Tutorial

43.268 Lượt nghe
Rust Programming: The Ultimate Builder Pattern Tutorial
Rust Programming Builder patterns tutorial and constructors patterns as well. We will also outline the pros & cons between the non-consuming and consuming builder pattern. Help support this channel: https://patreon.com/jeremychone Chapters: 00:00 - Intro 00:28 - Constructor Patterns 04:37 - Builder Patterns GitHub link: https://github.com/jeremychone-channel/rust-builder Bonus in the GitHub repo - State Builder Pattern (Video in the work) - https://github.com/jeremychone-channel/rust-builder/blob/main/src/web_states.rs Notes: 1) Struct literals and simple constructor functions are great ways to create structs and enums. 2) Builder pattern are useful when building the more complex data structure, especially when step building is required. 3) non-consuming-builder-pattern prop...(&mut self) and build(&self), can be considered more ergonomic, but would require unnecessary allocations when the object built needs to own the data passed to the builder. 4) non-consuming-builder-pattern with mutable build (i.e. build(&mut self) ) could be considered a bad pattern, as calling build() twice will give different result (except in the 6) case). 5) The consuming-builder-pattern requires reassigning the builder for each "builder chain command" and cloning if the builder needs to be reused. But it allows to avoid uncessary allocations. 6) In some situation, where the object built does not take any value from the builder, the non-consuming-builder-pattern might be appropriate. 7) Personally, other than 6), I find the Consuming-builder-pattern better aligned with the compiler ownership rules. New allocations become explicit by requiring clone, and the builder cannot be reused without explicit clone. 8) However, study the pros & cons of each pattern, and use what makes sense to you and your team for your given use-cases. 9) The "impl Into String " pattern is not related to constructor or builder patterns but can be used with any function. Use this pattern if you find it productive and relevant to your use case. This previous video: https://youtu.be/b0bgAYJDhhQ How I start my new project: https://youtu.be/oxx7MmN4Ib0 Reddit discussion about this video: https://www.reddit.com/r/rust/comments/105pkqy/rust_builder_pattern_tutorial_constructors/ Other Resources: - Great state builder [attern article: https://www.greyblake.com/blog/builder-with-typestate-in-rust/ (eventually I will do a video on the state builder pattern) Jeremy Chone: - Twitter - https://twitter.com/jeremychone - Discord On Rust - https://discord.gg/W2besKCzjx - AWESOME-APP - https://awesomeapp.dev - Rust Templates for building awesome applications. - Patreon - https://patreon.com/jeremychone - Any help is a big help (for Rust educational content) Feeback welcome, happy coding! Other popular Rust Programming videos: - Quick Start Code Layout - https://www.youtube.com/watch?v=oxx7MmN4Ib0&list=PL7r-PXl6ZPcCIOFaL7nVHXZvBmHNhrh_Q - AWESOME-APP Full Overview - Rust template for building Awesome Desktop Application: https://www.youtube.com/watch?v=BY_ZjPGqJJk&list=PL7r-PXl6ZPcCIOFaL7nVHXZvBmHNhrh_Q - Tauri Quick Introduction (Desktop App wit Rust Programming): https://www.youtube.com/watch?v=kRoGYgAuZQE&list=PL7r-PXl6ZPcCIOFaL7nVHXZvBmHNhrh_Q - Rust Web App tutorials series: https://www.youtube.com/watch?v=VIig9IcQ-w8&list=PL7r-PXl6ZPcCIOFaL7nVHXZvBmHNhrh_Q&list=PL7r-PXl6ZPcCLvwpdD2Vj1O4CyoFTiHKd - Rust Bevy Full Tutorial - Game Development with Rust: https://www.youtube.com/watch?v=j7qHwb7geIM&list=PL7r-PXl6ZPcCIOFaL7nVHXZvBmHNhrh_Q - Rust for Java Developers series: https://www.youtube.com/watch?v=iFdh4sPC5Tg&list=PL7r-PXl6ZPcD63DS2djSiz4SlXkaTfobc&index=2&t=654s Playlists: - Rust For Desktop App: https://youtube.com/playlist?list=PL7r-PXl6ZPcAQAi1lfVrtHCSbaJbwZ4O8 - Everything Rust Programming - Tutorials, Courses, Tips, Examples: https://youtube.com/playlist?list=PL7r-PXl6ZPcCIOFaL7nVHXZvBmHNhrh_Q - Rust Programming for Web Development: https://youtube.com/playlist?list=PL7r-PXl6ZPcCLvwpdD2Vj1O4CyoFTiHKd - Rust Courses: https://youtube.com/playlist?list=PL7r-PXl6ZPcB4jn1_VR3D8tSK9DxOaiQE - Furst for Java Developers: https://youtube.com/playlist?list=PL7r-PXl6ZPcD63DS2djSiz4SlXkaTfobc AWESOME-APP ➜ https://awesomeapp.dev - Rust Templates for building awesome applications. Rust AWESOME-APP GitHub - https://github.com/orgs/rust-awesome-app/repositories 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.