How to create a list of strings


#1

how to create a list of strings with values given at compile time. And these values are fixed so conceptually the object can be a constant too. It cant be a global object so it cant be static.
I tried to make it this way:

const numberth: [String;12] = [String::from("First"),String::from("Second"),String::from("Third"),String::from("Fourth"),String::from("Fifth"),String::from("Sixth"),String::from("Seventh"),String::from("Eighth"),String::from("Nineth"),String::from("Tenth"),String::from("Eleventh"),String::from("Twelveth")];
but it tells me that

calls in constants are limited to struct and enum

I also tried
const numberth: [String;12] = ["First","Second","Third","Fourth","Fifth","Sixth","Seventh","Eighth","Nineth","Tenth","Eleventh","Twelveth"];

but gave the following error

expected type std::string::String
found type &'static str


#2

Why do you need those to be Strings?

Maybe you should take a look at this page from the Rust book.


#3

Strings are a heap allocated type which const does not allow as getting a value of such a type requires an allocation, which would mean that every time you would refer to a constant, there would be 12 allocations.

Why cannot you use &'static str instead? I think it will be easier to provide a solution when you would provide information about why you need String in particular.


#4

There is no particular reason. I am a newbie to rust and I was trying to do the last exercise from Chapter 3 and i was unaware of what String are in rust. Thanks to @Ealhad 's link and @xfix that i understand Strings of rust better now.