MarketStatic dispatch
Company Profile

Static dispatch

In computing, static dispatch is a form of polymorphism fully resolved during compile time. It is a form of method dispatch, which describes how a language or environment will select which implementation of a method or function to use.

Examples
Consider the following Rust program: trait Pet { fn speak(&self); } struct Cat { name: String } impl Cat { fn new(name: String) -> Self { Cat { name } } } impl Pet for Cat { fn speak(&self) { println!("{} says Meow!", self.name); } } fn talk(pet: T) { pet.speak(); } fn main() { let pet = Cat::new(String::from("Simba")); talk(pet); } the Rust Compiler will monomorphize the program's code at compile time into: // [...] // struct Cat, Cat's impl, the Pet impl for struct Cat, and the Pet trait remain the same. fn talk_cat(pet: Cat) { Cat::speak(&pet) } fn main() { let pet = Cat::new(String::from("Simba")); talk_cat(pet); // talk(pet) gets replaced with the more specialized talk_cat } == See also ==
tickerdossier.comtickerdossier.substack.com