Generic associated types
Quick answer
A generic associated type (GAT) allows an associated type to have its own lifetime, type or const parameters. This can describe an output whose type depends on the lifetime of a borrow of the implementor.
Borrowed views
Declare type View<'a> where Self: 'a; and return Self::View<'_> from a method borrowing self. The outlives bound permits the associated type to borrow the implementor. An implementation chooses a concrete family of types, unlike a single ordinary associated type.
trait View { type Item<'a> where Self: 'a; fn view(&self) -> Self::Item<'_>; }impl View for String { type Item<'a> = &'a str; fn view(&self) -> Self::Item<'_> { self.as_str() }}fn main() { let s = String::from("abc"); println!("{}", s.view()); }Expected stdout (not executed):
abcThe view borrows the String without allocation.
trait View { type Item<'a> where Self: 'a; fn view(&self) -> Self::Item<'_>; }impl View for String { type Item<'a> = &'a str; fn view(&self) -> Self::Item<'_> { self.as_str() }}fn main() { let v; { let s = String::from("abc"); v = s.view(); } println!("{v}"); }Expected: compilation fails; exact diagnostic not verified.
A GAT describes the relationship but cannot extend source lifetime.
Common mistake
Traits containing GATs are not dyn-compatible on the selected baseline.
Try it yourself
Implement the same View trait for Vec<u8> with a borrowed byte slice output.
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
Generic associated types — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A trait's view type should borrow from the particular receiver lifetime. Which feature fits?
Generic associated types — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust trait View { type Item<'a> where Self: 'a; fn view(&self) -> Self::Item<'_>; } impl View for String { type Item<'a> = &'a str; fn view(&self) -> Self::Item<'_> { self.as_str() } } fn main() { let s = String::from("abc"); println!("{}", s.view()); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Generic associated types — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust trait View { type Item<'a> where Self: 'a; fn view(&self) -> Self::Item<'_>; } impl View for String { type Item<'a> = &'a str; fn view(&self) -> Self::Item<'_> { self.as_str() } } fn main() { let v; { let s = String::from("abc"); v = s.view(); } println!("{v}"); } ``` Which outcome or interpretation best matches this example?