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?
Correct.
> How do you implement map, fold, etc., that work on an entire collection regardless of length?
So, while arrays exist, there are also things like slices and vectors, which have a runtime length, rather than an array's compile-time length. So that's one bit of it. But the other part is that these are implemented on iterators, rather than on the type directly, so usually what happens is "turn that into an iterator, then do map/fold/filter/whatever on the iterator. It is easy to convert the compile-time length array to a run-time length iterator, so it ends up being a uniform interface.
However, often times it entirely compiles away to nothing. What this may help with is bounds checks; it is possible for this to make it easier to remove/hoist bounds checks in some circumstances. In my understanding.
[1,2,3].iter().map(|x| x + 1)
https://doc.rust-lang.org/std/iter/trait.Iterator.html