Joe Thomas
Joe Thomas

Reputation: 6417

Static mutable array not working in Rust

I have a global static array that I declared as a lookup table in Rust. For some odd reason I can't assign values to elements. It looks like this:

pub static mut WON_TABLE: &'static [u8] = &[0; 1000];

fn main () {
    for mov in 0..1000 {
        unsafe {
            WON_TABLE[mov as usize] = some_analyzer_function(mov);
        }
    }
}

For some reason this doesn't work and I keep getting the error:

error: cannot assign to immutable indexed content

Does anyone know why this is going on?

Upvotes: 4

Views: 2691

Answers (1)

Joe Thomas
Joe Thomas

Reputation: 6417

I just figured out the answer. I have to also declare the variables inside the array as mutable. I do this by changing:

pub static mut WON_TABLE: &'static [u8] = &[0; 1000];

to:

pub static mut WON_TABLE: &'static mut [u8] = &mut [0; 1000];

I hope this answer is useful to people who have similar problems in the future. If anyone else can expand on this, it'd be great! :D

Upvotes: 4

Related Questions