2

I have an array of strings like

const LANGUAGES: [&str; 3] = [
  "EN",
  "ES",
  "DE",
];

and I would like to lowercase them at compile time. How can I do that?

Gaston Sanchez
  • 1,160
  • 6
  • 13
  • 1
    Doing anything like this would require either a procedural macro or a build script, neither of which are super simple solutions. Perhaps the problem you're trying to solve can be tackled another way? – eggyal Jun 09 '21 at 23:44
  • Probably not what you really want, but you can use `lazy_static` to create static objects that run some initialization code the first time they're accessed. – Todd Jun 09 '21 at 23:56
  • What do you exactly want ? To have two separate arrays, one being built from the other one ? Or only one array in lowercase. If you're thinking about two separate arrays then you should consider using structs for consistency and code clarity. – Denys Séguret Jun 10 '21 at 06:39
  • Assuming you don't just do it manually because you have tons of text data that you copied into your sources... I'd just write a python script with regular expression substitutions to lowercase any strings in the sources that need it. It only needs to be done once... – Todd Jun 10 '21 at 08:03

1 Answers1

-3

You could do this:

const LANGUAGES: [&'static str; 3] = [
    "EN",
    "US",
    "DE",
];

It works, I've found an explanation of &'static type in this post. However, I do not understand what ' means in itself and why 'static does what it does.

But it works.

winwin
  • 550
  • 5
  • 15
  • 1
    explanation about `'static` is here: [What are the Rust types denoted with a single apostrophe?](https://stackoverflow.com/questions/22048673/what-are-the-rust-types-denoted-with-a-single-apostrophe) – Luuk Jan 29 '22 at 16:40