# rep_move
[](https://crates.io/crates/rep_move)
[](https://docs.rs/rep_move)
Replicating iterators: build N values from one source with N−1 clones — the
original is **moved** out on the final iteration, saving one clone for
heap-backed types like `Vec`, `String`, or `Arc`-wrapped data.
`#![no_std]`, zero dependencies.
## Example
```rust
use rep_move::RepMove;
let original = vec![1, 2, 3];
let copies: Vec<_> = RepMove::new(original, Vec::clone, 4).collect();
// 3 heap allocations instead of 4 — the last one was moved for free.
assert_eq!(copies.len(), 4);
```
## Types
| `RepMove` | `T` | Yes — `ExactSizeIterator` |
| `RepMap` | `U` | Yes — `ExactSizeIterator` |
| `DynamicRepMove` | `T` | No — the closure controls the count |
| `DynamicRepMap` | `U` | No — the closure controls the count |
## What std can't express: `RepMap`
`core::iter::repeat_n` covers the symmetric clone-then-move case. The
`*RepMap` types go further: the first N−1 items are built from `&T` and the
final one **consumes** `T`, both producing some other type `U` — the last
conversion reuses the source's resources instead of cloning them first:
```rust
use rep_move::RepMap;
let s = String::from("hello");
let boxes: Vec<Box<str>> = RepMap::new(
s,
|s: &String| s.as_str().into(), // N−1 items: one allocation each
String::into_boxed_str, // final item: zero allocations
3,
).collect();
```
The std route would have to clone the `String` first and convert afterwards,
paying an extra allocation for every item.
## O(1) skips
`last()`, `count()` and `nth()` on `RepMove` / `RepMap` never materialize
the skipped replicas — `iter.last()` is just the moved source, with zero
clones, exactly like `core::iter::repeat_n`.
Full API documentation lives in the source docs: <https://docs.rs/rep_move>
## Feature flags
- `nightly` *(disabled by default)* — makes the constructors `const fn` and
`Replicator` a `const trait`; additionally implements the unstable
`TrustedLen` contract and specializes the unstable `Iterator::advance_by`
for `RepMove` / `RepMap`. Requires a nightly compiler.
## License
Licensed under the [MIT license](LICENSE).