zip_clone

Crates.iozip_clone
lib.rszip_clone
version0.1.5
created_at2024-04-20 09:09:28.111742+00
updated_at2025-06-06 21:45:28.42045+00
descriptionZip an iterator to a repeatedly cloned object
homepage
repositoryhttps://github.com/jongiddy/zip_clone
max_upload_size
id1214467
size28,591
(jongiddy)

documentation

README

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 zip_clone::ZipClone as _;

let iter = vec![2, 3, 4].into_iter();
assert_eq!(
    iter.zip_clone("abc".to_string()).collect::<Vec<_>>(),
    vec![
        (2, "abc".to_string()),
        (3, "abc".to_string()),
        (4, "abc".to_string()),
    ]
);

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![String::new(); 10];
let hello = String::from("Hello");
for elem in v.iter_mut() {
    // `hello` cloned 10 times
    *elem = hello.clone();
}

clone the String 9 times using:

let mut v = vec![String::new(); 10];
let hello = String::from("Hello");
for (elem, hello) in v.iter_mut().zip_clone(hello) {
    // `hello` cloned 9 times, 1 element gets the original `hello`
    *elem = hello;
}

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(&email)
    .split(',')
    .zip_clone(String::from("Sent to "))
    .map(|(recepient, mut message)| {
        message.push_str(recepient);
        message
    })
    .collect::<Vec<String>>();

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 = String::from("Hello");
let _ = (0..10).zip_clone(hello).last();

For other methods, if possible, it is better to filter the iterator before adding zip_clone:

let mut v = vec![String::new(); 10];
let hello = String::from("Hello");
for (elem, hello) in v.iter_mut().take(5).zip_clone(hello) {
    // `hello` cloned 4 times, 1 element gets the original `hello`
    *elem = hello;
}
Commit count: 11

cargo fmt