beginnerCompound Types
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
This example demonstrates how to use array and slice in Rust. Read the code carefully to understand the flow. Pay attention to where values are created, borrowed, moved, or consumed.
Key Concepts
- Rust's strong type system catches errors at compile time
- Ownership and borrowing rules ensure memory safety
- Pattern matching makes code expressive and exhaustive
Related Topics
Browse more examples in the compound-types category to build a complete understanding of this topic.