What Is the Difference Between Associated Types and Generic Parameters?

Generic parameters are chosen by the caller, while associated types are defined by the trait implementer.

Generic parameters are placeholders for types provided by the caller, while associated types are placeholders for types defined by the implementer of a trait. Generic parameters appear in angle brackets on the struct or function definition, whereas associated types are declared inside a trait and resolved when the trait is implemented.

// Generic parameter: Caller chooses T
struct Point<T> {
    x: T,
    y: T,
}

// Associated type: Implementer chooses Item
trait Iterator {
    type Item;
    fn next(&mut self) -> Option<Self::Item>;
}

impl Iterator for Vec<i32> {
    type Item = i32; // Implementer defines the type
    fn next(&mut self) -> Option<Self::Item> { /* ... */ }
}