zip_clone
Zip an iterator to a repeatedly cloned value. Returns an iterator of 2-tuples containing an iterator item and a clone of the value.
use ZipClone as _;
let iter = vec!.into_iter;
assert_eq!;
One iteration returns the original value, using one fewer clones than
iter.zip(repeat_with(|| cloned.clone())).
Instead of cloning the String 10 times using:
let mut v = vec!;
let hello = Stringfrom;
for elem in v.iter_mut
clone the String 9 times using:
let mut v = vec!;
let hello = Stringfrom;
for in v.iter_mut.zip_clone
This is especially useful when an iterator commonly returns a single value, but can return more values, to avoid cloning for the common case:
let messages = get_email_recepients
.split
.zip_clone
.map
.;
zip_clone avoids cloning if items are skipped using methods including last, nth and skip.
The following code uses the original String for the single value produced, avoiding any cloning.
let hello = Stringfrom;
let _ = .zip_clone.last;
For other methods, if possible, it is better to filter the iterator before adding zip_clone:
let mut v = vec!;
let hello = Stringfrom;
for in v.iter_mut.take.zip_clone