mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-26 22:53:28 +00:00
Add basic usage example
This commit is contained in:
parent
bfb0279653
commit
c1df9f1b6b
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user