How to optimise write to file in Rust?

Viewed 593

I am new to rust. In past few days I learnt a lot from this community. With your help, I have made a code which reads 5000 files, do some transformation and write 700 files. However, I am still having issues with performance and I am trying to optimise. I could see that Writing to file was taking some time, so wanted to get an opinion on how to optimise it

//file_holder is hashmap
//file is a custom struct with 3 hashmap<String, f32>
file_holder.par_iter().for_each(|(name,file)| {
        serde_json::to_writer(&fs::File::create(name.to_string() +".json").expect("Failed writing"), &file).expect("Failed writing");
    });
1 Answers

Thanks to Rodrigo, It worked.

file_holder.par_iter().for_each(|(k,file)| {
        let f = fs::File::create(k.to_string() +".json").expect("Unable to create file");
        let mut bw = BufWriter::new(f);
        serde_json::to_writer(bw, &file).expect("Failed writing :(");
    });
Related