In rustc 1.0.0, I'd like to write a function that mutates a two dimensional array supplied by the caller. I was hoping this would work:
fn foo(x: &mut [[u8]]) { x[0][0] = 42; } fn main() { let mut x: [[u8; 3]; 3] = [[0; 3]; 3]; foo(&mut x); } It fails to compile:
$ rustc fail2d.rs fail2d.rs:7:9: 7:15 error: mismatched types: expected `&mut [[u8]]`, found `&mut [[u8; 3]; 3]` (expected slice, found array of 3 elements) [E0308] fail2d.rs:7 foo(&mut x); ^~~~~~ error: aborting due to previous error I believe this is telling me I need to somehow feed the function a slice of slices, but I don't know how to construct this.
It "works" if I hard-code the nested array's length in the function signature. This isn't acceptable because I want the function to operate on multidimensional arrays of arbitrary dimension:
fn foo(x: &mut [[u8; 3]]) { // FIXME: don't want to hard code length of nested array x[0][0] = 42; } fn main() { let mut x: [[u8; 3]; 3] = [[0; 3]; 3]; foo(&mut x); } tldr; any zero-cost ways of passing a reference to a multidimensional array such that the function use statements like $x[1][2] = 3;$?