RRust By Example

Array and Slice

Fixed-size arrays and dynamic slices in Rust.

Array and Slice

Fixed-size arrays and dynamic slices in Rust.

Difficulty

Beginner

Code

rust
fn main() {
    let a: [i32; 5] = [1, 2, 3, 4, 5];
    let slice = &a[1..3];
    println!("slice: {:?}", slice);

    let mut matrix = [[0; 3]; 2];
    matrix[0][1] = 1;
    println!("matrix: {:?}", matrix);
}

Explanation

Arrays have fixed size. Slices are references to contiguous sequences.

Key Concepts

  • Read the code carefully and understand the data flow
  • Try modifying the example to see how it changes behavior
  • Run this code in the Rust Playground

Related Topics

Browse more examples in the compound-types category to build a complete understanding of this topic.

More Compound Types Examples