Issue with passing 2D bool array as function parameter

Using @constCast is a bad advice, and leads to problems just like this. It only exists to allow calling libraries that haven’t used const-correctness in their API, and you will almost never encounter these.

Also I think it would be helpful to further explain the difference between an array and a slice:
An array is a flat container, there is no pointer, if you declare x: [7]bool then there will be 7 bools directly next to each other in memory in that location. Similarly with x: [3][7]bool there will 21 bools directly next to each other in memory.
A slice on the other hand is an indirect data type. You can think of it as
[]T == struct{ptr: [*]T, len: usize}
So the memory layout of a [][]bool is completely different to the memory layout of a [3][7]bool, despite the similarities of their syntax.

Now to solve your problem, I’d suggest you check out this post, which asked the same question: How do I pass a multidimensional slice to a generic function?

3 Likes