3

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?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Joe Thomas
  • 5,807
  • 6
  • 25
  • 36

1 Answers1

3

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

Joe Thomas
  • 5,807
  • 6
  • 25
  • 36
  • I'd "expand" on it by **don't do this**. Global variables are a *terrible* idea. If you are going to do it, you might as well do it in [a thread-safe manner](http://stackoverflow.com/q/27791532/155423). – Shepmaster Nov 26 '16 at 14:53
  • You're everywhere! – Joe Thomas Nov 26 '16 at 14:55