@@ -40,6 +40,10 @@ use std::fmt;
40
40
use std:: hash:: Hash ;
41
41
#[ cfg( feature = "use_std" ) ]
42
42
use std:: fmt:: Write ;
43
+ #[ cfg( feature = "use_std" ) ]
44
+ type VecIntoIter < T > = :: std:: vec:: IntoIter < T > ;
45
+ #[ cfg( feature = "use_std" ) ]
46
+ use std:: iter:: FromIterator ;
43
47
44
48
#[ macro_use]
45
49
mod impl_macros;
@@ -1762,10 +1766,14 @@ pub trait Itertools : Iterator {
1762
1766
FoldWhile :: Continue ( acc)
1763
1767
}
1764
1768
1765
- /// Collect all iterator elements into a sorted vector in ascending order.
1769
+ /// Sort all iterator elements into a new iterator in ascending order.
1766
1770
///
1767
1771
/// **Note:** This consumes the entire iterator, uses the
1768
- /// `slice::sort_by()` method and returns the sorted vector.
1772
+ /// `slice::sort()` method and returns the result as a new
1773
+ /// iterator that owns its elements.
1774
+ ///
1775
+ /// The sorted iterator, if directly collected to a `Vec`, is converted
1776
+ /// without any extra copying or allocation cost.
1769
1777
///
1770
1778
/// ```
1771
1779
/// use itertools::Itertools;
@@ -1776,17 +1784,25 @@ pub trait Itertools : Iterator {
1776
1784
/// "abcdef".chars());
1777
1785
/// ```
1778
1786
#[ cfg( feature = "use_std" ) ]
1779
- fn sorted ( self ) -> Vec < Self :: Item >
1787
+ fn sorted ( self ) -> VecIntoIter < Self :: Item >
1780
1788
where Self : Sized ,
1781
1789
Self :: Item : Ord
1782
1790
{
1783
- self . sorted_by ( Ord :: cmp)
1791
+ // Use .sort() directly since it is not quite identical with
1792
+ // .sort_by(Ord::cmp)
1793
+ let mut v = Vec :: from_iter ( self ) ;
1794
+ v. sort ( ) ;
1795
+ v. into_iter ( )
1784
1796
}
1785
1797
1786
- /// Collect all iterator elements into a sorted vector .
1798
+ /// Sort all iterator elements into a new iterator in ascending order .
1787
1799
///
1788
1800
/// **Note:** This consumes the entire iterator, uses the
1789
- /// `slice::sort_by()` method and returns the sorted vector.
1801
+ /// `slice::sort_by()` method and returns the result as a new
1802
+ /// iterator that owns its elements.
1803
+ ///
1804
+ /// The sorted iterator, if directly collected to a `Vec`, is converted
1805
+ /// without any extra copying or allocation cost.
1790
1806
///
1791
1807
/// ```
1792
1808
/// use itertools::Itertools;
@@ -1797,27 +1813,29 @@ pub trait Itertools : Iterator {
1797
1813
/// let oldest_people_first = people
1798
1814
/// .into_iter()
1799
1815
/// .sorted_by(|a, b| Ord::cmp(&b.1, &a.1))
1800
- /// .into_iter()
1801
1816
/// .map(|(person, _age)| person);
1802
1817
///
1803
1818
/// itertools::assert_equal(oldest_people_first,
1804
1819
/// vec!["Jill", "Jack", "Jane", "John"]);
1805
1820
/// ```
1806
1821
#[ cfg( feature = "use_std" ) ]
1807
- fn sorted_by < F > ( self , cmp : F ) -> Vec < Self :: Item >
1822
+ fn sorted_by < F > ( self , cmp : F ) -> VecIntoIter < Self :: Item >
1808
1823
where Self : Sized ,
1809
1824
F : FnMut ( & Self :: Item , & Self :: Item ) -> Ordering ,
1810
1825
{
1811
- let mut v: Vec < Self :: Item > = self . collect ( ) ;
1812
-
1826
+ let mut v = Vec :: from_iter ( self ) ;
1813
1827
v. sort_by ( cmp) ;
1814
- v
1828
+ v. into_iter ( )
1815
1829
}
1816
1830
1817
- /// Collect all iterator elements into a sorted vector .
1831
+ /// Sort all iterator elements into a new iterator in ascending order .
1818
1832
///
1819
1833
/// **Note:** This consumes the entire iterator, uses the
1820
- /// `slice::sort_by_key()` method and returns the sorted vector.
1834
+ /// `slice::sort_by_key()` method and returns the result as a new
1835
+ /// iterator that owns its elements.
1836
+ ///
1837
+ /// The sorted iterator, if directly collected to a `Vec`, is converted
1838
+ /// without any extra copying or allocation cost.
1821
1839
///
1822
1840
/// ```
1823
1841
/// use itertools::Itertools;
@@ -1828,22 +1846,20 @@ pub trait Itertools : Iterator {
1828
1846
/// let oldest_people_first = people
1829
1847
/// .into_iter()
1830
1848
/// .sorted_by_key(|x| -x.1)
1831
- /// .into_iter()
1832
1849
/// .map(|(person, _age)| person);
1833
1850
///
1834
1851
/// itertools::assert_equal(oldest_people_first,
1835
1852
/// vec!["Jill", "Jack", "Jane", "John"]);
1836
1853
/// ```
1837
1854
#[ cfg( feature = "use_std" ) ]
1838
- fn sorted_by_key < K , F > ( self , f : F ) -> Vec < Self :: Item >
1855
+ fn sorted_by_key < K , F > ( self , f : F ) -> VecIntoIter < Self :: Item >
1839
1856
where Self : Sized ,
1840
1857
K : Ord ,
1841
1858
F : FnMut ( & Self :: Item ) -> K ,
1842
1859
{
1843
- let mut v: Vec < Self :: Item > = self . collect ( ) ;
1844
-
1860
+ let mut v = Vec :: from_iter ( self ) ;
1845
1861
v. sort_by_key ( f) ;
1846
- v
1862
+ v. into_iter ( )
1847
1863
}
1848
1864
1849
1865
/// Collect all iterator elements into one of two
0 commit comments