I think you either need to do so with unsafe code, or you will need a runtime check that the two lengths are equal. Our you could use one vector if tuples, which is what I'd prefer, if you don't need to be able to take slices of the individual arrays.
struct Word {
both: Vec<(Alphabet, Diacritic)>,
}
This would give better cache locality, and be pretty simple and safe. It just isn't what you asked for.
If your aim is to do only a single heap allocation and store the length only once, then Box<[(Alphabet, Diacritic)]> is the closest you can get in safe Rust.
Rust arrays can't be used for this, since their length is always a constant hardcoded at compile time.
If you need this exact memory layout with both arrays inline, you will have to do it very low-level, same as in C: allocate a raw chunk of memory (std::alloc), cast pointers, calculate offsets and alignments by hand, and write the data (ptr::write) yourself.
There is an option I haven't seen yet, and that is to utilize the soon to be stabilizedĀ¹ min-const-generics feature.
Essentially you give the container type a const parameter of type usize and use that to initialize the lengths of the internal arrays fields i.e. something like this:
Hi all, I want to implement a data structure that contains two vector and ensure that vectors have equal length.
My solution seems to fit the requirements just fine. In addition, the array length doesn't become useless. Instead it statically guarantees the invariant of both arrays being equal in length, and gives the user of the struct control over the actual value of that length.
As the other comment pointed out, I meant the len field.
Except OP mentioned "vector", which usually has a dynamic length. Your solution might still work for OP, we don't know, but it doesn't completly fit the requirements.