Reputation: 1921
I commonly need to copy data between two iterables. Apart from the special case of slices, I found no suitable function in the standard library, so I tried writing my own one:
fn copy(source: /* ? */, target: /* ? */) {
for (s, t) in source.zip(target) {
*t = s.clone();
}
}
What would be the right choice of data types to make this function as versatile as possible?
Upvotes: 1
Views: 101
Reputation: 42492
s.clone()
that doesn't seem super useful as it limits you to clonable input items. The caller can just use cloned()
or copied()
to adapt the iterator.
The only really necessary constraint should be that you can assign source items to target items.
I guess it'd look something like
fn copy<S, SI, T, TI>(source: S, target: T)
where
S: Iterator<Item = SI>,
T: Iterator<Item = TI>,
TI: DerefMut<Target = SI>,
{
for (s, mut t) in source.zip(target) {
*t = s;
}
}
but frankly for the rare cases I need to do such a thing I'd rather just write the imperative loop.
Upvotes: 5