To initialize an array in Rust, you can use array literals or the array macro.
In this example,
numbers
with integer values using array literals.fn main() {
let numbers = [1, 2, 3, 4, 5];
for number in &numbers {
println!("{} ", number);
}
}
1 2 3 4 5
In this example,
fruits
with character values using the array!
macro.fn main() {
let fruits = array!['A', 'p', 'p', 'l', 'e'];
for fruit in fruits.iter() {
println!("{} ", fruit);
}
}
A p p l e
In this tutorial, we learned How to Initialize an Array in Rust language with well detailed examples.