Avoiding placeholders when compiling

Hi,

Situation is the following: I want to compile an executable with an array located in a separate txt file. The way I am doing it now is to put placeholders and have a third tool that reads both the source and the txt and utilized regex to creates a merged new source file that is then executed by calling a system cmd cargo run Example:

main.rs

fn main(){
   let  array: [i32; XXX] = [VVV];
}

file.txt

XXX= 5
VVV= 1 2 3 4 5

and then my parser reads main + file.txt and substitutes it with:

main.rs

fn main(){
   let  array: [i32; 5] = [1,2,3,4,5];
}

Is there a more elegant way to do this without using vectors and system calls ? The point is to get values inside the main in order to produce a new binary with values compiled... Any suggestions?

m

Depending on the format of the data you're including, you could use include_bytes!, include_str!, or include!.

Also, just to make sure you're aware, since I can't tell from your description, but you can create a build script which cargo will run automatically in order to do your preprocessing if you need it.

1 Like

Thnx I wasn't aware of the include!... This is exactly what I needed