Trait implemented for Vec - avoid duplications?

I am implementing a bunch of methods that work on Vecs. I have a trait for them.
The problem is that whereas normally Rust is fairly forgiving about casting Vecs to slices, not so in this context. Some of the methods throw up errors when implemented for Vec<f64> and yet others when implemented for &[f64].

I got around it by implementing my trait for both Vec<f64> and for &[f64]. That stopped the errors but, of course, it results in having to write all the method definitions twice.
Is there an idiomatic solution to this problem that will avoid the duplication?

I think it is a better idea to just implement it for slices, because Vecs can so easily be converted; slices are a more generic type.

You could also implement it for Vec and then just have it forward to the slice implementations, or implement it for T: IntoIterator (though that may have some problems with coherence rules).

Thanks but what do you mean exactly by "have it forward to the slice implementations"?

P.S. I have now found the solution: put .as_slice() everywhere in the code where it complains that vectors are not implemented, even though they are implemented for slices.
Seems rather pedantic but there you have it.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.