3 ms·
Rust will actually optimize a chain of string additions to a chain of `push_str`s. This isn't hardcoded, just how addition is implemented. fn add(mut self,
by Veedrac 9y ago
Rust will actually optimize a chain of string additions to a chain of `push_str`s. This isn't hardcoded, just how addition is implemented.
fn add(mut self, other: &str) -> String {
self.push_str(other);
self
}
https://github.com/rust-lang/rust/blob/master/src/liballoc/string.rs#L1749 https://github.com/rust-lang/rust/blob/master/src/liballoc/s...
Of course, upfront sizing is still advantageous, but you avoid the pathological n² case.