-
Notifications
You must be signed in to change notification settings - Fork 13.4k
IteratorExt: by_ref + take_while consumes one extra element #22802
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
Comments
So how would you expect
So when all items match the I suppose |
I think this is a somewhat general issue with fn main() {
let a = [1, 2, 3, 4, 5];
let mut it = a.iter();
{
let mut it2 = it.by_ref().peekable();
let _ = it2.peek();
}
for i in it {
println!("{}", i);
}
} |
Note: in the |
It's kind of a gotcha, but you're explicitly giving a mutable ref of your iterator to another object, so it's completely natural. What has been proposed before is a peeking version of take_while that can only be used on .peekable, that way you could use both take_while and still access the boundary iterator element afterwards. |
This is documented behavior of a stable method, so I'm going to give it a close. It is a bit of a gotcha, but it also makes sense. |
take_while
also consumes the first element that does NOT fullfill the condition.prints
The text was updated successfully, but these errors were encountered: