Rollup merge of #80805 - camelid:iter-by_ref-example, r=steveklabnik

Improve `Iterator::by_ref` example

I split the example into two: one that fails to compile, and one that
works. I also made them identical except for the addition of `by_ref`
so we don't confuse readers with random differences.

cc `@steveklabnik,` who is the one that added the previous version of this example
This commit is contained in:
Yuki Okushi 2021-04-24 03:44:02 +09:00 committed by GitHub
commit dcb4083ed9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1646,31 +1646,16 @@ 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);
///
/// 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);
///
/// // let's try that again
/// let a = [1, 2, 3];
///
/// 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);
///
/// // now this is just fine:
/// assert_eq!(iter.next(), Some(&3));
/// assert_eq!(iter.next(), None);
/// // 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"]);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
fn by_ref(&mut self) -> &mut Self