Conceptual
Login

Generics and Trait Bounds in Rust

Generics let you write one piece of code that works for many types, and a trait bound is the condition you attach saying which types qualify. Instead of copying a function for every kind of value, you write it once and state what the value must be able to do. The compiler then makes a specialised version for each type actually used, so there is no speed penalty.

Questions this Concept answers

  • Why does generic Rust code usually cost nothing at run time compared with a version hand-written for one type?