diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index 6653b960d8df..f294dd3c3e0f 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -1387,6 +1387,17 @@ fn from_iter>(iterator: I) -> Vec { let (lower, _) = iterator.size_hint(); let mut vector = Vec::with_capacity(lower); + // This function should be the moral equivalent of: + // + // for item in iterator { + // vector.push(item); + // } + // + // This equivalent crucially runs the iterator precisely once. The + // optimization below (eliding bound/growth checks) means that we + // actually run the iterator twice. To ensure the "moral equivalent" we + // do a `fuse()` operation to ensure that the iterator continues to + // return `None` after seeing the first `None`. let mut i = iterator.fuse(); for element in i.by_ref().take(vector.capacity()) { let len = vector.len();