I often see the pattern where I need to loop over a range and call a function that takes ownership of its argument. Since that function takes ownership of its argument, I must clone the value. For example:
let a = "hello".to_string();
for i in 0..10 {
print_me(a.clone());
}
fn print_me(s: String) {
println!("{}", s);
}
I would like to find a pattern where the value is cloned for all calls except the last one as to prevent a redundant clone.
The simplest approach is to add an explicit check on the loop index:
for i in 0..10 {
if i == 9 {
print_me(a);
} else {
print_me(a.clone());
}
}
But the compiler is not able understand the pattern and complains about the moved value during iteration.
I also tried to build an iterator that does the clones and move using repeat_with(), chain() and once() but the closure cosumes the value:
let iter = std::iter::repeat_with(|| a.clone())
.take(9)
.chain(std::iter::once(a))
What would be the easiest way to achieve this pattern?
Thanks!