How to declare the type of tow-dimentional array in function?


#1
fn main() {
    //error
    //how
    let enemys[[u32;2];4] = [[10, 2], [20, 1], [8, 6], [1, 1]];
    let why:[u32;3] = [2, 3, 4];
    let luk:u32 = 3;
    //how
    fn best_order(luk:u32, enemys:??) {
         println!("{}", enemys[0][0]);
    }
   
   
}


What should be the right replacement of [[u32;2];4] and ?? .


#2

The only error in the first part is that you forgot the : after enemys. The same type works fine in the function as well. If you want to type-erase the lengths of the arrays, then that’s going to be messier. The standard library doesn’t provide any way to deal with nested slices like that.


#3

:stuck_out_tongue:Thanks, I am so stupid!

fn main() {
    let enemys:[[u32; 2]; 4] = [[10, 2], [20, 1], [8, 6], [1, 1]];
    let luk:u32 = 3;
    fn best_order(luk:u32, enemys:[[u32; 2]; 4]) {
        
    }
    println!("{}", enemys[0][0]);
   
}

It works!