rust-syntax-generics
This skill contains shell command directives (!`command`) that may execute system commands. Review carefully before installing.
rust-syntax-generics
Generic programming in Rust: type parameters <T>, trait bounds, where clauses, const generics <const N: usize>, default generic types, and how monomorphization shapes the trade-off between zero-cost abstraction and binary size. Generics are a compile-time feature: every concrete instantiation produces its own machine code. Runtime polymorphism in Rust requires dyn Trait (see [[rust-syntax-trait-objects]]).
Cross-references: [[rust-syntax-traits]] (trait declarations, blanket impls, orphan rule) [[rust-syntax-trait-objects]] (dyn Trait runtime alternative) [[rust-syntax-lifetimes]] (lifetime parameters mixed with type parameters) [[rust-syntax-gats]] (Generic Associated Types, built on this skill) [[rust-core-language-versions]] (which generic features stabilized when).
When to use this skill
- User writes
fn foo<T>(x: T),struct Foo<T>,enum Result<T, E>, orimpl<T> Foo<T> - User needs to constrain a type parameter with a trait:
T: Display + Clone - User wonders whether to use a
whereclause vs inline bounds - User asks "is this expanded at compile time" / "what's the runtime cost of generics"
- User defines an array-size-generic API:
fn process<const N: usize>(buf: [u8; N]) - User encounters confusing error:
T does not live long enoughand over-applies'static - User asks about default generic types (
Vec<T, A = Global>,HashMap<K, V, S = RandomState>) - User mixes generic lifetime and type parameters:
struct Wrapper<'a, T>