Now that const generics and array destructuring are stable, in might be nice to add a trait to the std/core prelude like the following:
pub trait CloneMany<const N: usize>: Clone {
fn clone_many(self) -> [Self; N];
}
impl<T: Clone, const N: usize> CloneMany<N> for T {
fn clone_many(self) -> [Self; N] {
todo!()
}
}
#[derive(Clone)]
struct X {}
fn main() {
let x = X{};
let [first, second, third] = x.clone_many();
func1(first);
func2(second);
func3(third);
}
It's not necessary or anything, but it seems cleaner in my mind, and importantly prevents people from having to omit that final clone that unaligns everything.
If you're taking self by value, it might make more sense to call this into_clones or something similar. As is, it reads like a normal .clone() call, which takes &self. (Alternatively, it could act like a normal .clone call, but then you'd get a needless extra clone sometimes.)