@@ -1859,6 +1859,77 @@ pub trait Iterator {
1859
1859
try_process ( self , |i| i. collect ( ) )
1860
1860
}
1861
1861
1862
+ /// Collects all the items from an iterator into a collection.
1863
+ ///
1864
+ /// This method consumes the iterator and adds all its items to the
1865
+ /// passed collection. The collection is then returned, so the call chain
1866
+ /// can be continued.
1867
+ ///
1868
+ /// This is useful when you already have a collection and wants to add
1869
+ /// the iterator items to it.
1870
+ ///
1871
+ /// This method is a convenience method to call [Extend::extend](trait.Extend.html),
1872
+ /// but instead of being called on a collection, it's called on an iterator.
1873
+ ///
1874
+ /// # Examples
1875
+ ///
1876
+ /// Basic usage:
1877
+ ///
1878
+ /// ```
1879
+ /// #![feature(iter_collect_into)]
1880
+ ///
1881
+ /// let a = [1, 2, 3];
1882
+ /// let mut vec: Vec::<i32> = vec![0, 1];
1883
+ ///
1884
+ /// a.iter().map(|&x| x * 2).collect_into(&mut vec);
1885
+ /// a.iter().map(|&x| x * 10).collect_into(&mut vec);
1886
+ ///
1887
+ /// assert_eq!(vec![0, 1, 2, 4, 6, 10, 20, 30], vec);
1888
+ /// ```
1889
+ ///
1890
+ /// `Vec` can have a manual set capacity to avoid reallocating it:
1891
+ ///
1892
+ /// ```
1893
+ /// #![feature(iter_collect_into)]
1894
+ ///
1895
+ /// let a = [1, 2, 3];
1896
+ /// let mut vec: Vec::<i32> = Vec::with_capacity(6);
1897
+ ///
1898
+ /// a.iter().map(|&x| x * 2).collect_into(&mut vec);
1899
+ /// a.iter().map(|&x| x * 10).collect_into(&mut vec);
1900
+ ///
1901
+ /// assert_eq!(6, vec.capacity());
1902
+ /// println!("{:?}", vec);
1903
+ /// ```
1904
+ ///
1905
+ /// The returned mutable reference can be used to continue the call chain:
1906
+ ///
1907
+ /// ```
1908
+ /// #![feature(iter_collect_into)]
1909
+ ///
1910
+ /// let a = [1, 2, 3];
1911
+ /// let mut vec: Vec::<i32> = Vec::with_capacity(6);
1912
+ ///
1913
+ /// let count = a.iter().collect_into(&mut vec).iter().count();
1914
+ ///
1915
+ /// assert_eq!(count, vec.len());
1916
+ /// println!("Vec len is {}", count);
1917
+ ///
1918
+ /// let count = a.iter().collect_into(&mut vec).iter().count();
1919
+ ///
1920
+ /// assert_eq!(count, vec.len());
1921
+ /// println!("Vec len now is {}", count);
1922
+ /// ```
1923
+ #[ inline]
1924
+ #[ unstable( feature = "iter_collect_into" , reason = "new API" , issue = "94780" ) ]
1925
+ fn collect_into < E : Extend < Self :: Item > > ( self , collection : & mut E ) -> & mut E
1926
+ where
1927
+ Self : Sized ,
1928
+ {
1929
+ collection. extend ( self ) ;
1930
+ collection
1931
+ }
1932
+
1862
1933
/// Consumes an iterator, creating two collections from it.
1863
1934
///
1864
1935
/// The predicate passed to `partition()` can return `true`, or `false`.
0 commit comments