Add basic usage example

This commit is contained in:
Camelid 2021-02-13 21:25:41 -08:00
parent bfb0279653
commit c1df9f1b6b

View File

@ -1535,6 +1535,21 @@ pub trait Iterator {
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// let mut words = vec!["hello", "world", "of", "Rust"].into_iter();
///
/// // Take the first two words.
/// let hello_world: Vec<_> = words.by_ref().take(2).collect();
/// assert_eq!(hello_world, vec!["hello", "world"]);
///
/// // 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