To access array elements in Rust using index, you can use square brackets notation.
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,
chars
with character values.3
using indexing chars[2]
.fn main() {
let chars = ['a', 'b', 'c', 'd', 'e'];
println!("{}", chars[2]);
}
c
In this tutorial, we learned How to Access Array Elements using Index in Rust language with well detailed examples.