Skip to main content

Associated Types vs Generic Types

Associated types are an abstract way to define a name for what a type should be.

In the Generics section, we encountered an associated type when we defined the Mul trait as a bound:

fn square<T: Mul<Output = T> + Copy>(x: T)...

A closer look reveals an additional parameter in our trait bound declaration - Output. Output is an associated type, sometimes called an associated item, and is another feature of Rust.

Associated Types in Use

Associated types are specified as part of a trait. They are defined by moving type declarations within the trait as an Output type.

A prime example is Mul specifying any Output. Contrary to generics, this is not required to be enforced to a specific type by the compiler.

pub trait Mul<RHS = Self> {
type Output;
fn mul(self, rhs: RHS) -> Self::Output;
}

Associated Types vs Generics

While associated types and generics are used similarly and generate scalable code, there are some key differences between the two:

  • Associated types generally provide more flexibility than generics, but only within their specific trait's scope.

  • Associated types specify a trait's expected output's type, whereas generics are more the input types for a trait. Associated types represent the result of the trait's behavior on a given type.