Rust Generic Types, Traits, and Lifetimes - rFronteddu/general_wiki GitHub Wiki

Generics are abstract stand-ins for concrete types or other properties. Functions can take parameters of some generic type, instead of a concrete type like i32 or String, in the same way they take parameters with unknown values to run the same code on multiple concrete values. You can combine traits with generic types to constrain a generic type to accept only those types that have a particular behavior, as opposed to just any type. Lifetimes allow us to give the compiler enough information about borrowed values so that it can ensure references will be valid in more situations than it could without our help.

Generics allow us to replace specific types with a placeholder that represents multiple types to remove code duplication.