@@ -2463,6 +2463,47 @@ pub trait Iterator {
2463
2463
}
2464
2464
}
2465
2465
}
2466
+
2467
+ /// Collects all items from iterator into a collection.
2468
+ ///
2469
+ /// This method consumes the iterator and includes all its items to the
2470
+ /// passed collection. The collection is then returned, so the call chain
2471
+ /// can be continued. Collections can be passed and returned either by
2472
+ /// value or by mutable reference.
2473
+ ///
2474
+ /// This method is a counter-part of [Extend::extend](trait.Extend.html),
2475
+ /// but instead of being called on collection, it's called on iterator.
2476
+ ///
2477
+ /// # Examples
2478
+ /// Basic usage on collection passed by value
2479
+ ///
2480
+ /// ```
2481
+ /// let result = (3..5).collect_into(vec![1, 2]);
2482
+ /// assert_eq!(vec![1, 2, 3, 4], result);
2483
+ /// ```
2484
+ /// More complex usage on collection passed by mutable reference
2485
+ ///
2486
+ /// ```
2487
+ /// let primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43];
2488
+ /// let mut vec_3 = vec![33, 53];
2489
+ /// let vec_103 = primes.into_iter()
2490
+ /// .cloned()
2491
+ /// .filter(|p| p % 10 == 3)
2492
+ /// .collect_into(&mut vec_3)
2493
+ /// .iter()
2494
+ /// .map(|i| i + 100)
2495
+ /// .collect::<Vec<_>>();
2496
+ /// assert_eq!(vec![33, 53, 3, 13, 23, 43], vec_3);
2497
+ /// assert_eq!(vec![133, 153, 103, 113, 123, 143], vec_103);
2498
+ /// ```
2499
+ #[ unstable( feature = "collect_into" , issue = "0" ) ]
2500
+ fn collect_into < E > ( self , mut collection : E ) -> E where
2501
+ E : Extend < Self :: Item > ,
2502
+ Self : Sized
2503
+ {
2504
+ collection. extend ( self ) ;
2505
+ collection
2506
+ }
2466
2507
}
2467
2508
2468
2509
/// Select an element from an iterator based on the given "projection"
0 commit comments