@@ -142,6 +142,7 @@ pub mod structs {
142142 #[ cfg( feature = "use_std" ) ]
143143 pub use crate :: unique_impl:: { Unique , UniqueBy } ;
144144 pub use crate :: with_position:: WithPosition ;
145+ pub use crate :: zip_clones:: ZipClones ;
145146 pub use crate :: zip_eq_impl:: ZipEq ;
146147 pub use crate :: zip_longest:: ZipLongest ;
147148 pub use crate :: ziptuple:: Zip ;
@@ -233,6 +234,7 @@ mod tuple_impl;
233234mod unique_impl;
234235mod unziptuple;
235236mod with_position;
237+ mod zip_clones;
236238mod zip_eq_impl;
237239mod zip_longest;
238240mod ziptuple;
@@ -617,6 +619,41 @@ pub trait Itertools: Iterator {
617619 zip_eq ( self , other)
618620 }
619621
622+ /// Create an iterator which iterates over this iterator paired with clones of a given value.
623+ ///
624+ /// If the iterator has `n` elements, the zipped value will be cloned `n-1` times. This function
625+ /// is useful when the zipped value is expensive to clone and you want to avoid cloning it `n`
626+ /// unnecessary using the trivial following code:
627+ /// ```rust
628+ /// let it = [0, 1, 2, 3, 4].into_iter();
629+ /// let zipped = "expensive-to-clone".to_string();
630+ /// for a in it {
631+ /// let b = zipped.clone();
632+ /// // do something that consumes the expensive zipped value
633+ /// }
634+ /// ```
635+ /// Instead, you can use `zip_clones`:
636+ /// ```rust
637+ /// use itertools::Itertools;
638+ /// let it = [0, 1, 2, 3, 4].into_iter();
639+ /// let zipped = "expensive-to-clone".to_string();
640+ /// for (a, b) in it.zip_clones(zipped) {
641+ /// // do something that consumes the expensive zipped value
642+ /// }
643+ /// ```
644+ ///
645+ /// The [`repeat_n()`](crate::repeat_n) function can be used to create from a zipped value
646+ /// an iterator that also clones the value `n-1` times, but it require to know the number of
647+ /// elements in the iterator in advance.
648+ #[ inline]
649+ fn zip_clones < T > ( self , zipped : T ) -> ZipClones < Self , T >
650+ where
651+ Self : Sized ,
652+ T : Clone ,
653+ {
654+ zip_clones:: zip_clones ( self , zipped)
655+ }
656+
620657 /// A “meta iterator adaptor”. Its closure receives a reference to the
621658 /// iterator and may pick off as many elements as it likes, to produce the
622659 /// next iterator element.
0 commit comments