I have the following while loop that runs generate_user_key
for each of the file in the file_array
, and outputs the result. I would like to parallelize this such that an array of the generated keys is returned, and the process is executed in parallel instead of sequential to make it faster.
use std::process::Command;
//file_array definition here
let mut i = 0;
while (i<100) {
let generated_key = Command::new("generate_user_key")
.arg(file_array[i])
.output()
.expect("generate_user_key command failed to start");
println!("stdout: {}", String::from_utf8_lossy(&generated_key.stdout));
i=i+1;
}
What is the best way to implement this in rust?
If you want to loop over the array items using
rayon
then you can simply createinto_par_iter
and work on array itemsYou can also use
range
to loop over and use thecounter
asarray
indexExample using
thread