Index out of bounds while copying the value in 3D Array

I am copying some value from one array to another with 3-D array , but It is giving Index Out of Bounds , can anyone help me with this ....

fn main() {
    const IUNITS1: usize = 30;
    const IUNITS2: usize = 98;
    const TEMP: usize = 2;

    let mut tempXS = Array::<f32, _>::zeros((TEMP, IUNITS1, IUNITS2));
    let mut x: [[[f32; IUNITS1]; IUNITS2]; TEMP] = [[[0.0; IUNITS1]; IUNITS2]; TEMP];
    for i in 0..TEMP as usize {
        for j in 0..IUNITS1 as usize {
            for k in 0..IUNITS2 as usize {
                tempXS[[i, j, k]] = x[i][j][k];
                println!("{:?}", tempXS[[i, j, k]]);
Error : thread 'main' panicked at 'index out of bounds: the len is 30 but the index is 30', src/

You used IUNITS1 to declared the innermost array, but you're using j in 0..IUNITS1 as the middle index.


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.