Skip to content

Commit

Permalink
Added explainatory comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucretiel committed Dec 6, 2018
1 parent 823dd8c commit 811a2bf
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/liballoc/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1734,6 +1734,9 @@ impl FromIterator<String> for String {
fn from_iter<I: IntoIterator<Item = String>>(iter: I) -> String {
let mut iterator = iter.into_iter();

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

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

0 comments on commit 811a2bf

Please sign in to comment.