Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Rust arrays are not mutable

  • By default arrays are not mutable and thus we cannot change a value.
  • This example has a compilation error.
fn main() {
    let numbers: [i32; 3] = [10, 11, 12];

    println!("{:?}", numbers);

    numbers[0] = 30; // cannot assign to `numbers[_]`, as `numbers` is not declared as mutable
    println!("{:?}", numbers);
}