In Rust, you declare an array using square brackets and optionally specify the type and size.
In this example,
numbers
of type i32
and size 5
.fn main() {
let numbers: [i32; 5] = [1, 2, 3, 4, 5];
println!("{:?}", numbers);
}
[1, 2, 3, 4, 5]
In this example,
chars
of type char
and size 6
.fn main() {
let chars: [char; 6] = ['a', 'b', 'c', 'd', 'e', '\0'];
println!("{:?}", chars);
}
[a, b, c, d, e, \u{0}]
In this tutorial, we learned How to Declare an Array in Rust language with well detailed examples.