Comment

Comment by steveklabnik

https://rust-lang.github.io/rfcs/2000-const-generics.html is the design document, it may or may not be a good resource depending on your knowledge of Rust.

A classic example of them being used in Rust is to be generic over arrays, like what the blog post talks about. Arrays encode their length as part of the type; [T; N] is the type of an array of Ts that’s N elements long. In other words:

  fn foo<T>(array: [T; ?]) {
How do you define ? to work on any length? When const generics is implemented, you’d write

  fn foo<T, const N>(array: [T; N]) {
and now you can call this function on arrays of varying lengths.

There's a bit more to it than that, but that's the core of it. Does that help?

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton