Modifying a vector at compile time

I want to push elements to a vector by operating on a constant string using a loop at compile time. nightly rust is fine and I know about build.rs. The question is two folds:

  1. Is there any way of using for loops to loop over the constant string in const fn? If not, is there any elegant/fast alternative?
  2. How do I initiate an empty vector/iterator and add elements to it at compile time? Normally I use push inside for loop. The vector will be then stored inside the binary and available to use when I want to run later on thus saving on precious runtime.

If storing vector inside the binary is possible through build.rs at compile-time, then I will consider it as a viable solution.

It is currently not possible to allocate memory at compile time. There is a proposal to implement this though:

1 Like

Thanks, I also commented there just now. :blush:

You can also try proc-macro if you only use literals.

1 Like

I am using include_str! to import string from an external file during compilation. Don't know if it classifies as a string literal.

Will look into it. Have never used those before.