How to set the limitation for args

there is function as below:
fn myfunc(a : i64+f64+String) ->bool{

}

as shown above, I want the args type limited in scope : i64, f64, String,
how to write the code ?

further more, I think for those, mainly impl the Eq trait, so I added like this:
fn myfunc(a :impl std::cmp::Eq) ->bool{
if a == 2_i32{
//something ...
}
}
it warns, cannot compare i32 and Eq ..

I'm not sure if I understand your question well. Can you be more precise?

  • How many arguments will be passed to the function? One or more?
  • Which types would you want the arguments to have and/or should the function be polymorphic such that it accepts different types in the same argument position?

thanks advance,,

only one argument passed to this func,
type should be i64, f64 or String, only those three types.

  1. first thing to note, if the parameter's value is from a different data type, then all the data types must implement the same Trait.
  2. secondly, f64 not implement Eq for some security reason.
  3. Third, even if i64, f64, String implement PartialEq, they all have different type of PartialEq trait.
    For example, String implement PartialEq<String> trait but f64 implement PartialEq<f64> trait. BUT! String can be compared with &str because String also implement PartialEq<&str> trait.

now back to your question, I believe you need to do data type checking/parsing first before handing it over to the function.

1 Like

thanks so much for the detailed information,
i think i should firstly create a trait for those three types,,

Side note, please read this formatting post. In particular, put your code into code blocks like so:

```
fn myfunc(a :impl std::cmp::Eq) ->bool{ // ...
```

To make it much more readable:

fn myfunc(a :impl std::cmp::Eq) ->bool{
    if a == 2_i32{
        //something ...
    }
}
2 Likes

ok,thanks.