@@ -14,8 +14,8 @@ use alloc::{
14
14
string:: String ,
15
15
} ;
16
16
17
- #[ cfg( feature = "use_alloc" ) ]
18
17
use crate :: Itertools ;
18
+ use crate :: intersperse:: { Intersperse , IntersperseWith } ;
19
19
20
20
pub use crate :: adaptors:: {
21
21
interleave,
@@ -35,6 +35,41 @@ pub use crate::merge_join::merge_join_by;
35
35
#[ cfg( feature = "use_alloc" ) ]
36
36
pub use crate :: rciter_impl:: rciter;
37
37
38
+ /// Iterate `iterable` with a particular value inserted between each element.
39
+ ///
40
+ /// [`IntoIterator`] enabled version of [`Iterator::intersperse`].
41
+ ///
42
+ /// ```
43
+ /// use itertools::intersperse;
44
+ ///
45
+ /// itertools::assert_equal(intersperse((0..3), 8), vec![0, 8, 1, 8, 2]);
46
+ /// ```
47
+ pub fn intersperse < I > ( iterable : I , element : I :: Item ) -> Intersperse < I :: IntoIter >
48
+ where I : IntoIterator ,
49
+ <I as IntoIterator >:: Item : Clone
50
+ {
51
+ Itertools :: intersperse ( iterable. into_iter ( ) , element)
52
+ }
53
+
54
+ /// Iterate `iterable` with a particular value created by a function inserted
55
+ /// between each element.
56
+ ///
57
+ /// [`IntoIterator`] enabled version of [`Iterator::intersperse_with`].
58
+ ///
59
+ /// ```
60
+ /// use itertools::intersperse_with;
61
+ ///
62
+ /// let mut i = 10;
63
+ /// itertools::assert_equal(intersperse_with((0..3), || { i -= 1; i }), vec![0, 9, 1, 8, 2]);
64
+ /// assert_eq!(i, 8);
65
+ /// ```
66
+ pub fn intersperse_with < I , F > ( iterable : I , element : F ) -> IntersperseWith < I :: IntoIter , F >
67
+ where I : IntoIterator ,
68
+ F : FnMut ( ) -> I :: Item
69
+ {
70
+ Itertools :: intersperse_with ( iterable. into_iter ( ) , element)
71
+ }
72
+
38
73
/// Iterate `iterable` with a running index.
39
74
///
40
75
/// [`IntoIterator`] enabled version of [`Iterator::enumerate`].
0 commit comments