Associated types and constants
Quick answer
Associated items belong to a trait implementation or type. An associated type lets an implementation choose a related type, and an associated constant gives a named constant value.
Express related information
Declare type Item; inside a trait and assign it in an implementation. Refer to it as Self::Item inside the trait. Declare const LIMIT: usize; for a constant that each implementation supplies. An associated type is chosen for a particular trait implementation rather than on every method call.
trait Source { type Item; fn read(&self) -> Self::Item; }struct Counter;impl Source for Counter { type Item = u32; fn read(&self) -> u32 { 8 }}fn main() { println!("{}", Counter.read()); }Expected stdout (not executed):
8Counter defines the result type once.
trait Config { const LIMIT: usize; }struct Small;impl Config for Small { const LIMIT: usize = 4; }fn main() { println!("{}", Small::LIMIT); }Expected stdout (not executed):
4The constant belongs to the implementation.
Common mistake
An associated type declaration is not a value or field allocation.
Try it yourself
Implement Source for a text-reading model with Item set to String.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Associated types and constants — practice quiz
Choose one answer per question, then review the explanation.
How does an implementation choose an associated type Output?
What does `Self::Item` refer to inside a trait method signature?
What can a trait's associated const express?