Skip to content
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

Unclear doc on iterator.by_ref() #95143

Closed
Yuri6037 opened this issue Mar 20, 2022 · 1 comment · Fixed by #135987
Closed

Unclear doc on iterator.by_ref() #95143

Yuri6037 opened this issue Mar 20, 2022 · 1 comment · Fixed by #135987
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools

Comments

@Yuri6037
Copy link

Yuri6037 commented Mar 20, 2022

Today, I just got a huge problem internally in one of my crate. The bug is that the call to iterator.by_ref mutates/advances the iterator itself internally and that caused one of my software dependent on that carate to fail with an internal weird panic.

On the following documentation link: https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html#method.by_ref the behavior of the function is not very clear.

The doc currently reads:

Borrows an iterator, rather than consuming it.

This is useful to allow applying iterator adapters while still retaining ownership of the original iterator.

From this text it's not clear that using this function still mutates/advances the internal iterator, so calling count for example is still gonna put the iterator to the end and will break all subsequent calls, essentially ending in weird cases where somehow the number of items is 1 but the later call to last().unwrap() panics!

@zirconium-n
Copy link
Contributor

While you might be right this text is not clear (which I do not agree), the example clearly shows that the internal iterator still advances.

@GuillaumeGomez GuillaumeGomez added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label Mar 27, 2022
Kobzol added a commit to Kobzol/rust that referenced this issue Mar 11, 2025
@bors bors closed this as completed in bb2324a Mar 11, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 11, 2025
Rollup merge of rust-lang#135987 - hkBst:patch-20, r=joboet

Clarify iterator by_ref docs

fixes rust-lang#95143
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants