I have a build script that performs some http requests and constructs a Vec<(String, String)> of the responses. I need to pass this data to my main program but I'm not sure what the best way is. My current approach is to write to a source file as a [(&str, &str); N] in the build script and include it the main program:
// build.rs
let path = env::var("OUT_DIR").expect("no variable called OUT_DIR");
let path = Path::new(&path).join("tags.rs");
let mut output_file = File::create(&path).expect("Failed to create `tags.rs`");
writeln!(output_file, "[")?;
for line in &services_and_tags {
writeln!(output_file, "({:?}, {:?}),", line.0, line.1)?;
}
writeln!(output_file, "]")?;
// main.rs
let my_vec = include!(concat!(env!("OUT_DIR"), "/tags.rs")).to_vec();
This works but it feels very clunky. Moreover, since my IDE doesn't know what is being include!()ed, I don't get any type hints.
Are there better ways to do this? For my use case it's not important for the file to be human readable.