We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 3ed6184 commit 2fcb8b5Copy full SHA for 2fcb8b5
library/std/src/ffi/os_str.rs
@@ -1208,11 +1208,18 @@ impl<'a> Extend<&'a OsStr> for OsString {
1208
impl FromIterator<OsString> for OsString {
1209
#[inline]
1210
fn from_iter<I: IntoIterator<Item = OsString>>(iter: I) -> Self {
1211
- let mut buf = Self::new();
1212
- for s in iter {
1213
- buf.push(&s);
+ let mut iterator = iter.into_iter();
+
+ // Because we're iterating over `OsString`s, we can avoid at least
1214
+ // one allocation by getting the first string from the iterator
1215
+ // and appending to it all the subsequent strings.
1216
+ match iterator.next() {
1217
+ None => OsString::new(),
1218
+ Some(mut buf) => {
1219
+ buf.extend(iterator);
1220
+ buf
1221
+ }
1222
}
- buf
1223
1224
1225
0 commit comments