https://github.com/jongiddy/zip_clone
Zip an iterator to a repeately cloned object
https://github.com/jongiddy/zip_clone
Last synced: 4 months ago
JSON representation
Zip an iterator to a repeately cloned object
- Host: GitHub
- URL: https://github.com/jongiddy/zip_clone
- Owner: jongiddy
- License: apache-2.0
- Created: 2024-04-20T09:07:51.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-06-18T20:33:51.000Z (12 months ago)
- Last Synced: 2025-02-11T14:57:14.834Z (4 months ago)
- Language: Rust
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# zip_clone
Zip an iterator to a repeately cloned object.
Pass an owned object that implements `Clone` to create an iterator that zips
the original iterator with clones of the object.One iteration returns the original object, hence using one fewer clones than
`iter.zip(repeat_with(|| cloned.clone()))`.This is useful for loops where a value is cloned for each iteration, but is not
used after the iteration.Instead of cloning a value 10 times using:
```rust
let mut v = vec![];
let s = String::from("Hello");
for _ in 0..10 {
v.push(s.clone());
}
```
clone the value 9 times using:
```rust
use zip_clone::ZipClone as _;let mut v = vec![];
let s = String::from("Hello");
for (_, s) in (0..10).zip_clone(s) {
v.push(s);
}
```The object may be cloned even fewer times if items are skipped. For example,
`last()` consumes the iterator but returns the last value without cloning the
object for intermediate values:
```rust
use zip_clone::ZipClone as _;let mut v = vec![];
let s = String::from("Hello");
v.push((0..10).zip_clone(s).last());
```