-
Notifications
You must be signed in to change notification settings - Fork 13.3k
std: Add Vec::from_iter comment #22394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
r? @gankro (rust_highfive has picked a reviewer for you, use r? to override) |
@bors: r+ 42053 rollup y'all |
// 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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, I'm not super keen on this fuse
approach. In general it adds a check to every next
call, to protect against a case that is basically "unspecified behaviour" anyway (the iterator being shorter than its stated size_hint
).
I'd prefer adding a check after the loop:
// the iterator ran out of elements before filling the whole vector, so we're done.
if vector.len() < vector.capacity() { return vector }
since that is guaranteed to be only a single check and outside the inner loop, even if the optimiser can't remove it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea! I have updated and added an appropriate comment as well.
… r=brson Requested by Niko in rust-lang#22200 (and is good to have anyway)
… r=brson Requested by Niko in rust-lang#22200 (and is good to have anyway)
f8251e7
to
bebfa78
Compare
It appears this landed in a rollup but then wasn't auto-closed. re-r? @huonw |
let mut i = iterator.fuse(); | ||
for element in i.by_ref().take(vector.capacity()) { | ||
// This equivalent crucially runs the iterator precisely once. Below we | ||
// actually in theory run the iteartor twice (one without bounds checks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/ear/era/
r=me with the speeling fix. |
bebfa78
to
95a28c9
Compare
Requested by Niko in rust-lang#22200 (and is good to have anyway)
Requested by Niko in rust-lang#22200 (and is good to have anyway)
Requested by Niko in #22200 (and is good to have anyway)