To access array elements in Rust, you can use indexing with square brackets.
In this example,
numbers
with integer values.2
using indexing numbers[1]
.fn main() {
let numbers = [1, 2, 3, 4, 5];
println!("{}", numbers[1]);
}
2
In this example,
fruits
with string values.fn main() {
let fruits = ["Apple", "Banana", "Orange"];
for fruit in fruits.iter() {
println!("{}", fruit);
}
}
Apple Banana Orange
In this tutorial, we learned How to Access Array Elements in Rust language with well detailed examples.