Skip to content

Improve Iterator::by_ref example #80805

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

Merged
merged 4 commits into from
Apr 23, 2021
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 28 additions & 15 deletions library/core/src/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1538,31 +1538,44 @@ pub trait Iterator {
/// Basic usage:
///
/// ```
/// let a = [1, 2, 3];
/// let mut words = vec!["hello", "world", "of", "Rust"].into_iter();
///
/// let iter = a.iter();
/// // Take the first two words.
/// let hello_world: Vec<_> = words.by_ref().take(2).collect();
/// assert_eq!(hello_world, vec!["hello", "world"]);
///
/// let sum: i32 = iter.take(5).fold(0, |acc, i| acc + i);
/// // Collect the rest of the words.
/// // We can only do this because we used `by_ref` earlier.
/// let of_rust: Vec<_> = words.collect();
/// assert_eq!(of_rust, vec!["of", "Rust"]);
/// ```
///
/// This demonstrates a use case that needs `by_ref`:
///
/// ```compile_fail,E0382
/// let a = [1, 2, 3, 4, 5];
/// let mut iter = a.iter();
///
/// let sum: i32 = iter.take(3).fold(0, |acc, i| acc + i);
/// assert_eq!(sum, 6);
///
/// // if we try to use iter again, it won't work. The following line
/// // gives "error: use of moved value: `iter`
/// // assert_eq!(iter.next(), None);
/// // Error! We can't use `iter` again because it was moved
/// // by `take`.
/// assert_eq!(iter.next(), Some(&4));
/// ```
///
/// // let's try that again
/// let a = [1, 2, 3];
/// Now, let's use `by_ref` to make this work:
///
/// ```
/// let a = [1, 2, 3, 4, 5];
/// let mut iter = a.iter();
///
/// // instead, we add in a .by_ref()
/// let sum: i32 = iter.by_ref().take(2).fold(0, |acc, i| acc + i);
///
/// assert_eq!(sum, 3);
/// // We add in a call to `by_ref` here so `iter` isn't moved.
/// let sum: i32 = iter.by_ref().take(3).fold(0, |acc, i| acc + i);
/// assert_eq!(sum, 6);
///
/// // now this is just fine:
/// assert_eq!(iter.next(), Some(&3));
/// assert_eq!(iter.next(), None);
/// // And now we can use `iter` again because we still own it.
/// assert_eq!(iter.next(), Some(&4));
/// ```
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point, should I just remove the later example? I don't feel like it adds much that the basic usage example doesn't show.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@camelid do youw ant to update this without the last example? looks good enough to me without it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dylan-DPC OK, I removed the example. Do you want to review this now?

#[stable(feature = "rust1", since = "1.0.0")]
fn by_ref(&mut self) -> &mut Self
Expand Down