Hasty Briefsbeta

Bilingual

Variadic Generics ideas that won't work for Rust

10 months ago
  • #Programming Languages
  • #Rust
  • #Variadic Generics
  • Variadic generics in Rust would allow implementing traits for tuples with an arbitrary number of fields.
  • Three common but flawed proposals for variadic generics are discussed: using iterators, variadic recursion, and first-class types.
  • Using iterators fails because there's no guarantee tuple members implement the required trait, and mapping tuples is clunky.
  • Variadic recursion, inspired by C++, splits logic across multiple trait implementations, leading to poor developer experience and compiler challenges.
  • First-class types, inspired by Zig, would require massive changes to Rust's type system and introduce post-monomorphization errors.
  • The article concludes that variadic generics need a syntax for trait bounds on all tuple members and iteration over them with type safety.
  • The Rust lang team and community members endorse the need for proper variadic generics over the discussed alternatives.