Rust allows a true form of polymorphism through special forms of types implementing a trait. These are known as trait objects. Before we explain how Rust achieves polymorphism using trait objects, we need to understand the idea of dispatch.
True polymorphism using trait objects
Dispatch
Dispatch is a concept that emerged from the object-oriented programming paradigm, mainly in the context of one of its features called polymorphism. In the context of OOP, when APIs are generic or take parameters implementing an interface, it here has to figure out what method implementation to invoke on an instance of a type that's passed to the API. This process of method resolution in a polymorphic context is called dispatch, and invoking...