@@ -78,22 +78,23 @@ pub use std::iter as __std_iter;
78
78
/// The concrete iterator types.
79
79
pub mod structs {
80
80
pub use adaptors:: {
81
+ Batching ,
82
+ Coalesce ,
81
83
Dedup ,
82
84
Interleave ,
83
85
InterleaveShortest ,
84
- Product ,
85
- PutBack ,
86
- Batching ,
87
86
MapInto ,
88
87
MapResults ,
89
88
Merge ,
90
89
MergeBy ,
90
+ Positions ,
91
+ Product ,
92
+ PutBack ,
93
+ SetSizeHint ,
91
94
TakeWhileRef ,
92
- WhileSome ,
93
- Coalesce ,
94
95
TupleCombinations ,
95
- Positions ,
96
96
Update ,
97
+ WhileSome ,
97
98
} ;
98
99
#[ allow( deprecated) ]
99
100
pub use adaptors:: Step ;
@@ -1902,6 +1903,40 @@ pub trait Itertools : Iterator {
1902
1903
v. into_iter ( )
1903
1904
}
1904
1905
1906
+ /// Explicitly provide a size hint for an iterator.
1907
+ ///
1908
+ /// This can be useful in situations where the size hint cannot be deduced
1909
+ /// automatically, but where the programmer knows the bounds on the number
1910
+ /// of elements.
1911
+ ///
1912
+ /// ```
1913
+ /// use itertools::{self, Itertools};
1914
+ ///
1915
+ /// let data = vec![1, 2, 6, 7, 2];
1916
+ ///
1917
+ /// let result: Vec<i32> = data.iter()
1918
+ /// // The `FlatMap` adapter is not able to deduce the size hint itself
1919
+ /// // so `size_hint()` would return `(0, None)`.
1920
+ /// .flat_map(|&x| {
1921
+ /// let repeats = if x % 2 == 0 { 1 } else { 3 };
1922
+ /// itertools::repeat_n(x, repeats)
1923
+ /// })
1924
+ /// // But we know the bounds on the max and min number of items in the
1925
+ /// // resulting iterator, so we can provide that information:
1926
+ /// .set_size_hint(data.len(), Some(3 * data.len()))
1927
+ /// // The `Vec` should not excessively re-allocate, while collecting
1928
+ /// // since it now knows the minimum and maximum number of items.
1929
+ /// .collect();
1930
+ ///
1931
+ /// assert_eq!(result, vec![1, 1, 1, 2, 6, 7, 7, 7, 2]);
1932
+ /// ```
1933
+ ///
1934
+ fn set_size_hint ( self , min : usize , max : Option < usize > ) -> SetSizeHint < Self >
1935
+ where Self : Sized ,
1936
+ {
1937
+ adaptors:: set_size_hint ( self , min, max)
1938
+ }
1939
+
1905
1940
/// Collect all iterator elements into one of two
1906
1941
/// partitions. Unlike `Iterator::partition`, each partition may
1907
1942
/// have a distinct type.
0 commit comments