I am trying to initialize a Vec<String>
with some settings that can be reused over my code.
I am using const left: Vec<String> = vec![...
but this doesn't work:
error[E0308]: mismatched types
--> names-generator.rs:2:27
|
2 | const left: Vec<String> = vec![
| ^ expected slice, found array of 93 elements
|
= note: expected type `Box<[std::string::String]>`
= note: found type `Box<[&str; 93]>`
= note: this error originates in a macro outside of the current crate
What is the recommended way of doing something like this?
Do you want it to be mutable? Do the values have to be String
s? If the answer is "no" to both, you can use an array of string slices ([&str; N]
) instead of a Vec<String>
:
const LEFT: [&'static str; 3] = ["Hello", "World", "!"];
// or
const LEFT: &'static [&'static str] = &["Hello", "World", "!"];
const
s are basically copied wherever they are used, so the second form may be preferable depending on the size of the array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With