diff options
| author | Mattias Wallin <mattias.wallin@lidendata.com> | 2024-09-01 08:36:44 +0200 |
|---|---|---|
| committer | Yacin Tmimi <yacintmimi@gmail.com> | 2024-09-02 22:42:53 -0400 |
| commit | 9d407bfd1b69801d53c9c01dc8988f62d6235fed (patch) | |
| tree | 6c25bf5ab6d696cea219b82925c37e5f37f31a8a | |
| parent | 40909b4331a7bc481f5bcef149efa388f46222f7 (diff) | |
| download | rust-9d407bfd1b69801d53c9c01dc8988f62d6235fed.tar.gz rust-9d407bfd1b69801d53c9c01dc8988f62d6235fed.zip | |
Avoid allocating intermediate strings in `ItemizedBlock::trimmed_block_as_string`
| -rw-r--r-- | src/comment.rs | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/src/comment.rs b/src/comment.rs index c8cadf364da..1e35395cfb3 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -533,10 +533,11 @@ impl ItemizedBlock { /// Returns the block as a string, with each line trimmed at the start. fn trimmed_block_as_string(&self) -> String { - self.lines - .iter() - .map(|line| format!("{} ", line.trim_start())) - .collect::<String>() + self.lines.iter().fold(String::new(), |mut acc, line| { + acc.push_str(line.trim_start()); + acc.push(' '); + acc + }) } /// Returns the block as a string under its original form. |
