Skip to content

Commit 4b4efcc

Browse files
committed
Add fixed size collection example to Iterator::collect_into
1 parent f1eef5c commit 4b4efcc

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/libcore/iter/iterator.rs

+11
Original file line numberDiff line numberDiff line change
@@ -2496,6 +2496,17 @@ pub trait Iterator {
24962496
/// assert_eq!(vec![33, 53, 3, 13, 23, 43], vec_3);
24972497
/// assert_eq!(vec![133, 153, 103, 113, 123, 143], vec_103);
24982498
/// ```
2499+
/// Collecting part of iterator into a vector with manually set capacity
2500+
///
2501+
/// ```
2502+
/// let mut iter = 1..5;
2503+
/// let first_2 = iter.by_ref()
2504+
/// .take(2)
2505+
/// .collect_into(Vec::with_capacity(2));
2506+
/// let the_rest = iter.collect::<Vec<_>>();
2507+
/// assert_eq!(vec![1, 2], first_2);
2508+
/// assert_eq!(vec![3, 4], the_rest);
2509+
/// ```
24992510
#[unstable(feature = "collect_into", issue = "0")]
25002511
fn collect_into<E>(self, mut collection: E) -> E where
25012512
E: Extend<Self::Item>,

0 commit comments

Comments
 (0)