&[T] not detected as vector

i bit confusing, about generic type when i applied this code below why rust compiler gave me error. "std::cmp::PartialOrd" can someone explain this!

fn largest_func<T:   std::cmp::PartialOrd> (list: &[T]) -> &T {  }  // working properly with no error.

    fn largest_func<T> (list: &[T]) -> &T {    // error compiler

        let mut largest = &list[0];

        for item in list {
            if item > largest {
                largest = item;


Well, as written your function accepts slices containing elements of any type T. However, not every T can be compared with the > relation. Thus, you have to restrict T such that it implements the trait which makes the > relation available (or implement your function without >). That trait is PartialOrd.


It's usually a good idea to post the full compiler error. It contains useful information that makes it easier for us to understand the problem:

error[E0369]: binary operation `>` cannot be applied to type `&T`
 --> src/lib.rs:6:17
6 |         if item > largest {
  |            ---- ^ ------- &T
  |            |
  |            &T
help: consider restricting type parameter `T`
1 | fn largest_func<T: std::cmp::PartialOrd> (list: &[T]) -> &T {    // error compiler
  |                  ^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error


As @LingMan notes, the error comes from the comparison on line 6, because you haven't required that T supports that operation. Often, the compiler also has a suggestion about how you might correct the problem; that's the section that starts help: . If you rewrite line 1 to look like the compiler has printed it, this particular error will be taken care of.

It's important to note that this will sometimes uncover additional errors, and the resulting code still won't compile for some other reason. This doesn't mean that the suggestion was wrong, only incomplete.

1 Like

so generic implementation not automaticaly pass the std trait implementation for vector.

You're not trying to compare vectors (slices, really), you're trying to compare the elements within a slice. There's no particular reason why values need to be comparable to store them in a slice / vector: There's no sensible way to compare two File objects, for instance, but that shouldn't prevent you from having a list of open files.

Note that even if you were trying to compare the vectors, you still would have to restrict the inner type, since vectors can be compared only if the inner values can (see where clause).


assert_eq!(Point { x: 1, y: 0 } + Point { x: 2, y: 3 }, Point { x: 3, y: 3 });
makes me understand what code mean. deeply my way of thinking is like other language but in rust addition, comparison and etc have own trait implementation.


Thanks, Mr. LingMan and Mr Cerberuser i finally know inner scope and the reason trait can have different evaluation when it passes to generic type.

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.