0

I have a function which is called quite frequently which needs the letters of the alphabet in a vector.

To do so during runtime es quite easy:

let alphabet = (b'a'..b'z').map(|c| c as char).map(|c| c.to_string());
// Do some stuff with the alphabet...

Ok one can create another function creating this alphabet and then applying memorization on top of it...

...but I have heard that rust has a powerful macro feature.

How to create this vector - or maybe better a fixed size array with items of type str - during compile-time?

In other words I do want to have something like this

const alphabet: [&'static str; 26] = ["a", "b", ...];

auto generated.

Or is it better not to concern about this kind of optimization because the compiler does this anyway?

Rob
  • 14,746
  • 28
  • 47
  • 65
Matthias
  • 1,055
  • 2
  • 14
  • 26

1 Answers1

4

If the content, you want to generate at compile time, is complex and you are not comfortable hardcoding it by hand, then you could write a build script for it. The build script would then generate the "hardcoded" rust code and write it into a file. Then you would include the generated file from your usual code.

chpio
  • 896
  • 6
  • 16