To declare a string array in Rust, you can use array initialization syntax or create a Vec (vector) of strings.
In this example,
string_array
using array initialization syntax with predefined string values.fn main() {
let string_array = ["Hello", "World", "Rust"];
for str in &string_array {
println!("{}", str);
}
}
Hello World Rust
In this example,
empty_string_array
.fn main() {
let empty_string_array: Vec<&str> = Vec::new();
for str in &empty_string_array {
println!("{}", str);
}
}
In this tutorial, we learned How to Declare a String Array in Rust language with well detailed examples.