Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why isn't 'impl Trait' syntax not working in function I created?

I created a function in a sample rust project that compares two values of types that implement the PartialOrd trait. It isn’t compiling. Below is the sample code:

fn main(){
    let x: i32 = 5;
    let y: i32 = 6;
    dbg!(greater_than(&x, &y));
}

fn greater_than(x: &impl PartialOrd, y: &impl PartialOrd) -> bool{
    x > y
}

I am using RustRover IDE (EAP). I also use Vs code. I am getting the error below

error[E0308]: mismatched types
 --> src/main.rs:8:9
  |
7 | fn greater_than(x: &impl PartialOrd, y: &impl PartialOrd) -> bool{
  |                     ---------------      --------------- found type parameter
  |                     |
  |                     expected type parameter
8 |     x > y
  |         ^ expected type parameter `impl PartialOrd`, found a different type parameter `impl PartialOrd`
  |
  = note: expected reference `&impl PartialOrd` (type parameter `impl PartialOrd`)
             found reference `&impl PartialOrd` (type parameter `impl PartialOrd`)
  = note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound
  = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters

My understanding is that once in your parameters in your function signature were typed using the impl trait syntax, any type that implement that trait (i32 is meant to implement PartialOrd trait) could be used in the function as an argument. (I have used a different function signature -using trait bounds as shown below- and it worked fine):
fn greater_than<T: PartialOrd>(x: &T, y: &T) -> bool

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

My understanding is that once in your parameters in your function signature were typed using the impl trait syntax, any type that implement that trait (i32 is meant to implement PartialOrd trait) could be used in the function as an argument.

Well yes, but impl trait defines no relation between your arguments, so what you’ve written is essentially

fn greater_than<T: PartialOrd, U: PartialOrd>(x: &T, y: &U) -> bool

So each argument is partially comparable to itself, but there’s no implication that they’re partially comparable to one another. For all Rust is concerned, you could pass an i32 as first parameter and a String as second, they both impl PartialOrd.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading