GAT Collections
Tutorial Video
Text description (accessibility)
This video demonstrates the "GAT Collections" functional Rust example. Difficulty level: Advanced. Key concepts covered: Functional Programming. A truly generic collection trait in Rust — one where `map` changes the element type — requires GATs. Key difference from OCaml: 1. **Native vs. GAT**: OCaml's `'a t` is a native parameterized type constructor; Rust requires the GAT feature to express the same concept in traits.
Tutorial
The Problem
A truly generic collection trait in Rust — one where map changes the element type — requires GATs. Without them, you cannot express "a Vec<T> mapped by T -> U produces Vec<U>" in a trait that also applies to HashMap<K, V>, BTreeSet<T>, and custom containers. GAT collections resolve this by making the container's output type generic over the element type, enabling unified generic algorithms over diverse collection types.
🎯 Learning Outcomes
type Mapped<U> in collection traits, changing element types through mapFunctor for collections is expressed using GATs in RustVec<T> and Option<T>Code Example
#![allow(clippy::all)]
// Stub — awaiting conversion from OCaml source.Key Differences
'a t is a native parameterized type constructor; Rust requires the GAT feature to express the same concept in traits.COLLECTION signature is terse; Rust's equivalent requires explicit type Item, type Mapped<U>, and where bounds.HashMap<K, V> requires handling two type parameters; GATs handle this with type Mapped<K2, V2>.OCaml Approach
OCaml expresses this naturally:
module type COLLECTION = sig
type 'a t
val map : ('a -> 'b) -> 'a t -> 'b t
val filter : ('a -> bool) -> 'a t -> 'a t
end
module ListCollection : COLLECTION with type 'a t = 'a list = struct ... end
The parameterized type 'a t is OCaml's built-in equivalent of Rust's GAT type Mapped<U>. This pattern is fundamental to OCaml's standard library design.
Full Source
#![allow(clippy::all)]
// Stub — awaiting conversion from OCaml source.Deep Comparison
OCaml vs Rust: Gat Collections
Overview
See the example.rs and example.ml files for detailed implementations.
Key Differences
| Aspect | OCaml | Rust |
|---|---|---|
| Type system | Hindley-Milner | Ownership + traits |
| Memory | GC | Zero-cost abstractions |
| Mutability | Explicit ref | mut keyword |
| Error handling | Option/Result | Result<T, E> |
See README.md for detailed comparison.
Exercises
Collection for Option<T> with type Mapped<U> = Option<U>.flat_map_col<U>(self, f: impl Fn(Self::Item) -> Self::Mapped<U>) -> Self::Mapped<U> method to the trait.double_all<C: Collection<Item = i32>>(c: C) -> C::Mapped<i32> that doubles every element.