Z
Z
ziplane2019-05-23 14:25:05
Rust
ziplane, 2019-05-23 14:25:05

Why does panic come out - panicked at 'panicked at 'index out of bounds: the len is 0 but the index is 0'?

Can you explain or give a link why panic comes out. In theory, everything is correct.

let matrix_a =  vec![1,2,3,4];
    let matrix_b =  vec![5,6,7,8];
    let mut  matrix_c = vec![];


    for row in 0..2 {
        for  col in 0..2 {
            let mut vec_buf = 0;
            for k in 0..2 {
                vec_buf = vec_buf + matrix_a[row*2 +k] * matrix_b[k * 2 +col]

            }

            //matrix_c.push(vec_buf)

            matrix_c[row *2 +col]= vec_buf // здесь место паники
        }
    }

    for (i,x) in matrix_c.iter().enumerate(){
        println!(" элем = {:?}, значение = {:?}",i,x)
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Somewhere Intech, 2019-05-23
@ziplane

The vector has been initialized, but no memory has been allocated. Do this:
let mut matrix_c = vec![type;size];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question