Array of strings in rust_lang

how to make an array of strings

["foo", "bar", "baz", "quux"]

1 Like

not like that it wand to declare data type before Initialization
then insert elements

Then you probably want a vector, not an array:

let mut elements = vec![];
elements.insert("foo");
elements.insert("quux");

That will give you a Vec<&'static str>. If you want a vector of owned strings, then:

let mut elements = vec![];
elements.insert("foo".to_string());
elements.insert("quux".to_string());
1 Like

Arrays need to be initialized all at once. If you want dynamic insertion, either use a standard Vec or the external arrayvec::ArrayVec

without using vector
any other way is possible

Arrays have fixed length, so push and insert don't even make sense. For simple Copy-able types like &str, you could initialize an array with placeholder values and then overwrite them.

let mut array = [""; 3];
array[0] = "foo";
array[1] = "bar";
array[2] = "baz";

how to make a character array is it possible ..?

Please describe the high level problem you're trying to solve.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.