Our first program will demonstrate how to work with arrays in Rust. Here’s the full source code with explanations:
To run the program, save it as arrays.rs and use rustc to compile, then run the resulting binary:
Note that arrays in Rust are fixed-size and allocated on the stack. For more flexible, dynamically-sized sequences, Rust provides Vec<T> (vector), which is similar to std::vector in C++ or ArrayList in Java.
Arrays in Rust are zero-indexed and bounds-checked at runtime. Accessing an array out of bounds will result in a runtime panic, helping to catch and prevent buffer overflow vulnerabilities.
Rust’s type system and ownership rules provide strong guarantees about the correct use of arrays, making them safe and efficient to use in systems programming contexts.