๐ Code With Me in Rust: Implementing the Strategy Pattern with Boxed Trait Objects
Continuing in my exploration of Rust, in this video, I learn how to store boxed trait objects inside Rust structs as a way of embedding code in a struct. I'm referencing the โboxed trait objectโ example from the Stack Overflow post I found in my first video of this series (link in the resources section below). Then I use this technique to implement Aliens that can have varying recovery behaviors in a shooting game. ๐ง Topics Covered: - What is a trait object? - Why "dyn"? - What does "+ 'static" mean? - Why does Box required the trait object to be 'static? - Capturing things and the effects of that "static" requirement - Benefits of this technique This video is for intermediate to advanced Rust developers who want to write more flexible and performant code. ๐ 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:28 The example 00:45 What is a Trait Object? 01:15 Boxing and the dyn keyword 01:47 The constructor 01:54 The static keyword 02:52 Why Box wants "static" 03:11 Using our new struct 03:43 Modifying Alien's struct 06:25 Modifying Alien's implementation 06:43 Using closures that don't capture anything 08:16 What happens when we do capture something 09:22 Why that "static" limits what we can do 12:45 Summarize so far 13:53 Now we can make a list of Aliens 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.