To learn Rust syntax, I've decided to implement a function sorting a passed-in array:
fn sort(array) {
// actual sorting
}
In this question I found out how to pass an array and change its content, but besides this the array must consist of types that can be compared. I've found the std::cmp::PartialOrd
trait and figured out that the elements of an array need to implement it.
By connecting this knowledge with the paragraph about dynamic dispatch in the Rust book I've built something like this:
use std::cmp;
fn sort(arr: &mut [&std::cmp::PartialOrd]) {
// actual sorting
}
This doesn't compile:
error[E0393]: the type parameter `Rhs` must be explicitly specified
--> src/lib.rs:3:21
|
3 | fn sort(arr: &mut [&std::cmp::PartialOrd]) {
| ^^^^^^^^^^^^^^^^^^^^ missing reference to `Rhs`
|
= note: because of the default `Self` reference, type parameters must be specified on object types
Is there a correct syntax to achieve passing an array of objects implementing a certain trait to a function?