To create a vector of strings in Rust, you can use the vec! macro or manually initialize a vector with string elements.
In this example,
vec!
macro followed by square brackets []
to create a vector of strings.vec!
macro generates a vector with the specified strings.str_vector
.let str_vector = vec!["apple", "banana", "cherry", "date", "grape"];
println!("{:?}", str_vector);
["apple", "banana", "cherry", "date", "grape"]
In this example,
str_vector
of type Vec
.push
method to add strings one by one to the vector.let mut str_vector: Vec<String> = Vec::new();
str_vector.push("apple".to_string());
str_vector.push("banana".to_string());
str_vector.push("cherry".to_string());
str_vector.push("date".to_string());
str_vector.push("grape".to_string());
println!("{:?}", str_vector);
["apple", "banana", "cherry", "date", "grape"]
In this tutorial, we learned How to create a Vector of Strings in Rust language with well detailed examples.