Generic Values?!


I have been trying to write some Rust code for a couple of days now, and to my surprise, I noticed Rust does not allow for generic values (please inform me about correct terminology):

struct A<T, S>([T; S]);

This makes implementing wrappers around fixed arrays really difficult. I cannot implement Debug and Clone is a pain, just because rust doesn’t support these generics. I have to write an entire macro to pretty much generate my program, for a generic size of an array.

Why is it that Rust doesn’t allow for this? What is holding Rust back? Is there an RFC or any other discussions?


The problem is that Rust sees arrays as a fixed length array known at compile time. Simply put, you either have to use Vec<T> or wrap your own array implementation in unsafe code (which I would highly discourage). The Rust developers (as I gather) implemented Vec<T> so that they could handle all of the memory issues for developers. Some other users have expressed concerns for this approach. You can read the thread here and see a blog response by another user about unsafe code


Example workaround:


arrayvec is what I use now. It only works around a small portion of the issues though. Issues like not being able to implement Default on wrapper types of arrays bigger than 32 indices without having to write a custom fmt are the real deal breakers.