简体   繁体   中英

How to optimise write to file in Rust?

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");
    });

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 :(");
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM