Find Related products on Amazon

Shop on Amazon

Exploring Dynamic Dispatch in Rust

Published on: 2025-05-24 17:36:51

Let me preface this by saying that I am a novice in the world of rust (though I'm liking things so far!), so if I make technical mistakes please let me know and I will try to correct them. With that out of the way, lets get started. My real motivation for taking a closer look at dynamic dispatch can be seen in the following code snippet. Suppose I want to create a struct CloningLab that contains a vector of trait objects (in this case, Mammal ): This works fine. You can iterate over the vector of subjects and call run or walk as you would expect. However, things break down when you try to add an additional trait to the trait object bounds like: This fails with the the following error: error [ E0225 ] : only the builtin traits can be used as closure or object bounds --> test1 . rs : 3 : 32 | 3 | subjects : Vec < Box < Mammal + Clone >> , | ^^^^^ non - builtin trait used as bounds And I found this surprising. In my mind, a trait object with multiple bounds would be analogous to mult ... Read full article.