@@ -38,6 +38,10 @@ use std::fmt;
38
38
use std:: hash:: Hash ;
39
39
#[ cfg( feature = "use_std" ) ]
40
40
use std:: fmt:: Write ;
41
+ #[ cfg( feature = "use_std" ) ]
42
+ type VecIntoIter < T > = :: std:: vec:: IntoIter < T > ;
43
+ #[ cfg( feature = "use_std" ) ]
44
+ use std:: iter:: FromIterator ;
41
45
42
46
#[ macro_use]
43
47
mod impl_macros;
@@ -1609,10 +1613,14 @@ pub trait Itertools : Iterator {
1609
1613
FoldWhile :: Continue ( acc)
1610
1614
}
1611
1615
1612
- /// Collect all iterator elements into a sorted vector in ascending order.
1616
+ /// Sort all iterator elements into a new iterator in ascending order.
1613
1617
///
1614
1618
/// **Note:** This consumes the entire iterator, uses the
1615
- /// `slice::sort_by()` method and returns the sorted vector.
1619
+ /// `slice::sort()` method and returns the result as a new
1620
+ /// iterator that owns its elements.
1621
+ ///
1622
+ /// The sorted iterator, if directly collected to a `Vec`, is converted
1623
+ /// without any extra copying or allocation cost.
1616
1624
///
1617
1625
/// ```
1618
1626
/// use itertools::Itertools;
@@ -1623,17 +1631,25 @@ pub trait Itertools : Iterator {
1623
1631
/// "abcdef".chars());
1624
1632
/// ```
1625
1633
#[ cfg( feature = "use_std" ) ]
1626
- fn sorted ( self ) -> Vec < Self :: Item >
1634
+ fn sorted ( self ) -> VecIntoIter < Self :: Item >
1627
1635
where Self : Sized ,
1628
1636
Self :: Item : Ord
1629
1637
{
1630
- self . sorted_by ( Ord :: cmp)
1638
+ // Use .sort() directly since it is not quite identical with
1639
+ // .sort_by(Ord::cmp)
1640
+ let mut v = Vec :: from_iter ( self ) ;
1641
+ v. sort ( ) ;
1642
+ v. into_iter ( )
1631
1643
}
1632
1644
1633
- /// Collect all iterator elements into a sorted vector .
1645
+ /// Sort all iterator elements into a new iterator in ascending order .
1634
1646
///
1635
1647
/// **Note:** This consumes the entire iterator, uses the
1636
- /// `slice::sort_by()` method and returns the sorted vector.
1648
+ /// `slice::sort_by()` method and returns the result as a new
1649
+ /// iterator that owns its elements.
1650
+ ///
1651
+ /// The sorted iterator, if directly collected to a `Vec`, is converted
1652
+ /// without any extra copying or allocation cost.
1637
1653
///
1638
1654
/// ```
1639
1655
/// use itertools::Itertools;
@@ -1644,27 +1660,29 @@ pub trait Itertools : Iterator {
1644
1660
/// let oldest_people_first = people
1645
1661
/// .into_iter()
1646
1662
/// .sorted_by(|a, b| Ord::cmp(&b.1, &a.1))
1647
- /// .into_iter()
1648
1663
/// .map(|(person, _age)| person);
1649
1664
///
1650
1665
/// itertools::assert_equal(oldest_people_first,
1651
1666
/// vec!["Jill", "Jack", "Jane", "John"]);
1652
1667
/// ```
1653
1668
#[ cfg( feature = "use_std" ) ]
1654
- fn sorted_by < F > ( self , cmp : F ) -> Vec < Self :: Item >
1669
+ fn sorted_by < F > ( self , cmp : F ) -> VecIntoIter < Self :: Item >
1655
1670
where Self : Sized ,
1656
1671
F : FnMut ( & Self :: Item , & Self :: Item ) -> Ordering ,
1657
1672
{
1658
- let mut v: Vec < Self :: Item > = self . collect ( ) ;
1659
-
1673
+ let mut v = Vec :: from_iter ( self ) ;
1660
1674
v. sort_by ( cmp) ;
1661
- v
1675
+ v. into_iter ( )
1662
1676
}
1663
1677
1664
- /// Collect all iterator elements into a sorted vector .
1678
+ /// Sort all iterator elements into a new iterator in ascending order .
1665
1679
///
1666
1680
/// **Note:** This consumes the entire iterator, uses the
1667
- /// `slice::sort_by_key()` method and returns the sorted vector.
1681
+ /// `slice::sort_by_key()` method and returns the result as a new
1682
+ /// iterator that owns its elements.
1683
+ ///
1684
+ /// The sorted iterator, if directly collected to a `Vec`, is converted
1685
+ /// without any extra copying or allocation cost.
1668
1686
///
1669
1687
/// ```
1670
1688
/// use itertools::Itertools;
@@ -1675,22 +1693,20 @@ pub trait Itertools : Iterator {
1675
1693
/// let oldest_people_first = people
1676
1694
/// .into_iter()
1677
1695
/// .sorted_by_key(|x| -x.1)
1678
- /// .into_iter()
1679
1696
/// .map(|(person, _age)| person);
1680
1697
///
1681
1698
/// itertools::assert_equal(oldest_people_first,
1682
1699
/// vec!["Jill", "Jack", "Jane", "John"]);
1683
1700
/// ```
1684
1701
#[ cfg( feature = "use_std" ) ]
1685
- fn sorted_by_key < K , F > ( self , f : F ) -> Vec < Self :: Item >
1702
+ fn sorted_by_key < K , F > ( self , f : F ) -> VecIntoIter < Self :: Item >
1686
1703
where Self : Sized ,
1687
1704
K : Ord ,
1688
1705
F : FnMut ( & Self :: Item ) -> K ,
1689
1706
{
1690
- let mut v: Vec < Self :: Item > = self . collect ( ) ;
1691
-
1707
+ let mut v = Vec :: from_iter ( self ) ;
1692
1708
v. sort_by_key ( f) ;
1693
- v
1709
+ v. into_iter ( )
1694
1710
}
1695
1711
1696
1712
/// Collect all iterator elements into one of two
0 commit comments