BorrowCheck › Rust language core
Associated types, GATs, const generics
Associated types vs type parameters, generic associated types, const generics.
The topic continues the discussion of traits and explains the items a trait ties to an implementing type: associated types, associated constants and const generics. The questions check how an associated type differs from a trait's type parameter: the implementation picks the result type itself and there can be only one per type, a trait object must specify Item explicitly, and ambiguity is resolved with a qualified path. The subtopic on generic associated types (GATs) covers a lending iterator whose items borrow from the iterator itself, the required where Self: 'a clause, and why such a trait loses dyn compatibility. The const generics subtopic covers the allowed parameter types, argument order, expressions in braces, an implementation for arrays of any length, and the fact that different values of N produce different types. The last subtopic is about associated constants: default values, use in match patterns and as an array length, and access through a trait bound. The topic builds on traits and generics.
45 questions: 18 basic, 22 intermediate, 5 advanced. 4 info pages, 14 sources.
Subtopics
- Associated types vs type parameters
- Generic associated types (GATs)
- Const generics
- Associated constants
Previous: Traits, generics, dyn / impl · Next: Collections and iterators