@@ -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,43 @@ 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` times,
626+ /// using the trivial following code:
627+ /// ```rust
628+ /// let it = [0, 1, 2, 3, 4].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+ /// drop((a, b));
634+ /// }
635+ /// ```
636+ /// Instead, you can use `zip_clones`:
637+ /// ```rust
638+ /// use itertools::Itertools;
639+ /// let it = [0, 1, 2, 3, 4].iter();
640+ /// let zipped = "expensive-to-clone".to_string();
641+ /// for (a, b) in it.zip_clones(zipped) {
642+ /// // do something that consumes the expensive zipped value
643+ /// drop((a, b));
644+ /// }
645+ /// ```
646+ ///
647+ /// The [`repeat_n()`](crate::repeat_n) function can be used to create from a zipped value
648+ /// an iterator that also clones the value `n-1` times, but it require to know the number of
649+ /// elements in the iterator in advance.
650+ #[ inline]
651+ fn zip_clones < T > ( self , zipped : T ) -> ZipClones < Self , T >
652+ where
653+ Self : Sized ,
654+ T : Clone ,
655+ {
656+ zip_clones:: zip_clones ( self , zipped)
657+ }
658+
620659 /// A “meta iterator adaptor”. Its closure receives a reference to the
621660 /// iterator and may pick off as many elements as it likes, to produce the
622661 /// next iterator element.
0 commit comments