diff options
| author | varkor <github@varkor.com> | 2018-05-21 18:57:49 +0100 |
|---|---|---|
| committer | varkor <github@varkor.com> | 2018-05-21 18:57:54 +0100 |
| commit | 2fa22effb6e1dd3b3e2e587ec5fcabefe2eb3443 (patch) | |
| tree | 512d4d77b9d2669e2eef75b8e0ee55d7c3450998 /src/liballoc/str.rs | |
| parent | c51f00280205d476651ff9f9a46cff6645b411a2 (diff) | |
| download | rust-2fa22effb6e1dd3b3e2e587ec5fcabefe2eb3443.tar.gz rust-2fa22effb6e1dd3b3e2e587ec5fcabefe2eb3443.zip | |
Avoid counting characters and add explanatory comment to test
Diffstat (limited to 'src/liballoc/str.rs')
| -rw-r--r-- | src/liballoc/str.rs | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/src/liballoc/str.rs b/src/liballoc/str.rs index 8af14d3c698..823e56b64e3 100644 --- a/src/liballoc/str.rs +++ b/src/liballoc/str.rs @@ -380,7 +380,13 @@ impl str { reason = "return type may change to be an iterator", issue = "27791")] pub fn escape_debug(&self) -> String { - self.chars().enumerate().flat_map(|(i, c)| c.escape_debug_ext(i == 0)).collect() + let mut string = String::with_capacity(self.len()); + let mut chars = self.chars(); + if let Some(first) = chars.next() { + string.extend(first.escape_debug_ext(true)) + } + string.extend(chars.flat_map(|c| c.escape_debug_ext(false))); + string } /// Escapes each char in `s` with [`char::escape_default`]. |
