4

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?

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
You're everywhere!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.