I have a JSON file containing some data I need in several functions of my application. Currently I just read the file from my hard drive every time I need it.
use serde_json::Value;
pub fn data_from_file() -> Vec<Value> {
serde_json::from_str::<Value>(
&std::fs::read_to_string("data.json").unwrap()
).unwrap().as_array().unwrap().clone()
}
pub fn a_function() {
let data = data_from_file();
// do something with the data
}
However, I suppose this is kind of inefficient. The file content does not change while the program is running. So it should be sufficient to read the file once at the start of the program and store it somewhere for future use. But I'm not sure what or where this "somewhere" could be. I thought about global variables, but using them in Rust seems to be quite complicated and I am not convinced if this is really the best way to go. So, are there any best practices how to solve this kind of problem?
Thank you in advance for any answers.