Skip to content

Commit c1df9f1

Browse files
committed
Add basic usage example
1 parent bfb0279 commit c1df9f1

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

library/core/src/iter/traits/iterator.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1535,6 +1535,21 @@ pub trait Iterator {
15351535
///
15361536
/// # Examples
15371537
///
1538+
/// Basic usage:
1539+
///
1540+
/// ```
1541+
/// let mut words = vec!["hello", "world", "of", "Rust"].into_iter();
1542+
///
1543+
/// // Take the first two words.
1544+
/// let hello_world: Vec<_> = words.by_ref().take(2).collect();
1545+
/// assert_eq!(hello_world, vec!["hello", "world"]);
1546+
///
1547+
/// // Collect the rest of the words.
1548+
/// // We can only do this because we used `by_ref` earlier.
1549+
/// let of_rust: Vec<_> = words.collect();
1550+
/// assert_eq!(of_rust, vec!["of", "Rust"]);
1551+
/// ```
1552+
///
15381553
/// This demonstrates a use case that needs `by_ref`:
15391554
///
15401555
/// ```compile_fail,E0382

0 commit comments

Comments
 (0)