Skip to content

Commit 811a2bf

Browse files
authored
Added explainatory comments
1 parent 823dd8c commit 811a2bf

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/liballoc/string.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1734,6 +1734,9 @@ impl FromIterator<String> for String {
17341734
fn from_iter<I: IntoIterator<Item = String>>(iter: I) -> String {
17351735
let mut iterator = iter.into_iter();
17361736

1737+
// Because we're iterating over `String`s, we can avoid at least
1738+
// one allocation by getting the first string from the iterator
1739+
// and appending to it all the subsequent strings.
17371740
match iterator.next() {
17381741
None => String::new(),
17391742
Some(mut buf) => {
@@ -1749,6 +1752,9 @@ impl<'a> FromIterator<Cow<'a, str>> for String {
17491752
fn from_iter<I: IntoIterator<Item = Cow<'a, str>>>(iter: I) -> String {
17501753
let mut iterator = iter.into_iter();
17511754

1755+
// Because we're iterating over CoWs, we can (potentially) avoid at least
1756+
// one allocation by getting the first item and appending to it all the
1757+
// subsequent items.
17521758
match iterator.next() {
17531759
None => String::new(),
17541760
Some(cow) => {

0 commit comments

Comments
 (0)