How to create a static array of strings? How to create a static array of strings? arrays arrays

How to create a static array of strings?


This is a stable alternative for Rust 1.0 and every subsequent version:

const BROWSERS: &'static [&'static str] = &["firefox", "chrome"];


There are two related concepts and keywords in Rust: const and static:

https://doc.rust-lang.org/reference/items/constant-items.html

For most use cases, including this one, const is more appropriate, since mutation is not allowed, and the compiler may inline const items.

const STRHELLO:&'static str = "Hello";const STRWORLD:&'static str = "World";const ARR:[&'static str, ..2] = [STRHELLO,STRWORLD];

Note, there is some out-of-date documentation out there that doesn't mention the newer const, including Rust by Example.


Another way to do it nowadays is:

const A: &'static str = "Apples";const B: &'static str = "Oranges";const AB: [&'static str; 2] = [A, B]; // or ["Apples", "Oranges"]