-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
explain why the side effects of peek
matter
#33269
Comments
It is said:
Doesn't that sound explicit enough? |
Why do the side effects matter to the user of the API? What is it that they must watch out for that could affect them negatively? |
@tshepang If your iterator does things that have side-effects when This isn't an issue for most iterators, but if you are using iterators as an abstraction over a state machine, then the behavior could be confusing. |
So this is about custom iterators, not iterators in the stdlib? |
I don't think that there are any really weird iterators in the stdlib, but you can make them using the stdlib very easily. let new_iter = my_vector.iter().map(|a| { println!("hi"); a }); Now you have an iterator with a side-effect that will behave weirdly when peeked at. |
Oh, so side effect means doing stuff other than fetch the next value, that being writing to stdout in your example? |
That's what "side effect" means in this context - the effects of calling a function other than its value being returned. |
Unexpected side-effects in the stdlib might include blocking and early consumption of values from channel and I/O iterators. |
It sounds like there might be confusion about the term "side effect" here, which I don't think is fixable in our docs. But maybe being more explicit could help. @tshepang how are you feeling about this issue? Do you have any suggestions as to how this could be improved, now that you see what's going on? |
We can add a bit to the text, e.g.:
If that's not satisfactory, I would not mind if this issue was closed. |
Sounds good to me! |
Clarify "side effect" in peek's docs Fixes rust-lang#33269 /cc @tshepang
Clarify "side effect" in peek's docs Fixes rust-lang#33269 /cc @tshepang
Why does one have to be aware of the side effects? See #33016 for the change.
The text was updated successfully, but these errors were encountered: