capypad
0 day streak
rust / beginner
Snippet

Working with Fixed-Size Arrays

Arrays in Rust have a fixed length and must contain elements of the same type. They are useful when you want your data allocated on the stack rather than the heap.

snippet.rs
rust
1
2
3
4
5
6
fn main() {
let a: [i32; 3] = [10, 20, 30];
let first = a[0];
let length = a.len();
println!("First: {first}, Length: {length}");
}
Breakdown
1
let a: [i32; 3] = [10, 20, 30];
Declares an array of exactly three i32 integers.
2
let first = a[0];
Accesses the first element using zero-based indexing.
3
a.len()
Returns the number of elements in the array.