🔍 Code With Me in Rust: Implementing the Strategy Pattern with Unboxed Closures
Continuing in my exploration of Rust, in this video, I dive deep into how to store unboxed closures inside Rust structs, referencing the “unboxed closure” example from the Stack Overflow post I found in my last video (link in the resources section below). Following that example, I originally only stored function pointers because I didn't understand some important facts about the Fn. FnMut,and FnOnce traits and how those related to closures and function pointers. Also, closures in Rust can be tricky because of their anonymous types, how they capture data from their environment and how the "move" option changes how they capture things. I show how to store them in a struct without resorting to boxing or trait objects, by leveraging generic parameters. In developing this video, I got a lot of help from Lucas Baumann. Check out his gitlab to see more Rust projects: https://github.com/luca3s 🔧 Topics Covered: - Understanding why closures have unique, anonymous types - Techniques for storing unboxed closures in structs via generics - How "move" changes the way a closure captures its environment - Implementing the Strategy Pattern in Rust - Practical example: aliens in a game with customizable recovery logic via stored closures - Trade-offs between boxed and unboxed closures in terms of performance, ergonomics, and code complexity This video is for intermediate to advanced Rust developers who want to write more flexible and performant code without heap allocations or dynamic dispatch. 🔗 Resources: - How do I store a closure in a struct: https://stackoverflow.com/questions/27831944/how-do-i-store-a-closure-in-a-struct-in-rust 📌 Chapters: 00:00 Introduction 00:31 Explaining the original example 03:20 Modifying the example to capture a variable 05:52 Effects of the "move" option on a closure 06:56 Every closure has its own type 10:02 Why the struct has to have a generic 11:16 How move affects ownership 14:57 Starting my Alien recovery example 15:21 Separating what is recoverable 16:53 Storing an unboxed closure for recovery behavior 21:25 Creating Aliens 22:25 Implementing debug for my Alien struct 24:23 Using my Alien struct to recover 25:37 Making those closures capture something 27:37 How "move" changes it 31:42 Summary of how this works 33:00 The weakness of this approach! 34:46 Conclusion and next steps Don't forget to like, subscribe, and hit the bell icon for more programming explorations! 👍 If this helped you think like a developer, hit like, subscribe, and tap that bell 🔔 so you don’t miss the next part! CHECK OUT MY OTHER VIDEOS: Rust Programming Playlist: https://www.youtube.com/playlist?list=PLZLC5AM_r9AjYwkudkKvO0yHX3zv3sDV2 C Programming Playlist: https://www.youtube.com/playlist?list=PLZLC5AM_r9Ag0CZdGTUm3FWhBDX28B0Dj Intro to Java Playlist: https://www.youtube.com/playlist?list=PLZLC5AM_r9Ajo6LH_-g7dXjZemaQ5QXnV Software Engineering Topics Playlist: https://www.youtube.com/playlist?list=PLZLC5AM_r9AgMHq42vUXHkXEws3JWdnDb
Download
0 formatsNo download links available.