Skip to content

Commit a35824b

Browse files
alexcrichtonManishearth
authored andcommitted
std: Add Vec::from_iter comment
Requested by Niko in rust-lang#22200 (and is good to have anyway)
1 parent 81bce52 commit a35824b

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/libcollections/vec.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,17 @@ impl<T> FromIterator<T> for Vec<T> {
13871387
let (lower, _) = iterator.size_hint();
13881388
let mut vector = Vec::with_capacity(lower);
13891389

1390+
// This function should be the moral equivalent of:
1391+
//
1392+
// for item in iterator {
1393+
// vector.push(item);
1394+
// }
1395+
//
1396+
// This equivalent crucially runs the iterator precisely once. The
1397+
// optimization below (eliding bound/growth checks) means that we
1398+
// actually run the iterator twice. To ensure the "moral equivalent" we
1399+
// do a `fuse()` operation to ensure that the iterator continues to
1400+
// return `None` after seeing the first `None`.
13901401
let mut i = iterator.fuse();
13911402
for element in i.by_ref().take(vector.capacity()) {
13921403
let len = vector.len();

0 commit comments

Comments
 (0)