Generics and dynamic dispatch
There are two ways to write code that works for more than one type: the compiler writes a copy per type, or the value carries a pointer to its methods. Rust does both, and choosing wrongly costs you binary size or speed.
Eight articles, and the last one is the table that tells you which to pick.
Articles
Section titled “Articles”Nothing published in this chapter yet — all 8 articles are still to come.
Coming up (8)
- What is the difference between impl Trait and a generic parameter?what you can and cannot name
- How to return impl Trait from a functionhiding the concrete type on the way out
- What is a trait object (&dyn Trait)?one type, decided at runtime
- What is static dispatch vs dynamic dispatch?monomorphisation versus a vtable
- When should you use Box<dyn Trait>?heterogeneous collections and plugin points
- How to use trait bounds with where clausesbounds that stay readable
- How to write a generic functionthe type parameter, and the bound it needs
- How to use const genericsa length that is part of the type