rustfoldpre-allocation

Building a string using fold, and would like to speed it up by assigning the correct capacity


I have an implementation in a Rust struct that builds a very large string that will ultimately be written to a file. I wondered if this could be sped up by ensuring the string variable has the appropriate capacity, but it doesn't work in the way I expected.

Here's what I did:

let sum = izip!(self.ts, self.x, self.y).fold(String::with_capacity(capacity), |acc, x| {
            format!("{}{}, {}, {}\n", acc, x.0, x.1, x.2)
        });

I thought that the string would be accumulated into the first argument, so I instantiated it with the necessary capacity. However, by checking the capacity of the string that is output from this (sum), I can see that my assumptions were wrong.

Can someone help me understand the way in which my understanding of fold is wrong? And perhaps give a hint as to a better way to do build this string?


Solution

  • Using format! will always produce a new String. Instead you can write! to the existing string:

    let sum = izip!(self.ts, self.x, self.y).fold(String::with_capacity(capacity), |mut acc, x| {
        let _ = write!(acc, "{}, {}, {}\n", x.0, x.1, x.2);
        acc
    });
    

    Make sure to import std::fmt::Write as well.