mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-26 07:25:48 +00:00
std: Add Vec::from_iter comment
Requested by Niko in #22200 (and is good to have anyway)
This commit is contained in:
parent
81bce5290f
commit
a35824b149
@ -1387,6 +1387,17 @@ impl<T> FromIterator<T> for Vec<T> {
|
||||
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();
|
||||
|
Loading…
Reference in New Issue
Block a user