Look into traits. You can define a trait and then impl it on all the types you want the function to handle. You can then update your function to accept any type which implements the defined trait instead of a specific type.
Read more about traits and using them here (has examples too).
yes i can use trait. One thing i don't understand is, the struct im talking about is just stores output from sql query and returns. I don't have need of any method to implement on this struct type.
still i can use trait. or any other way.
Rust doesn't knows how and why you are using fields. If the run function will only work with Strings you may prefer using Into<String> as parameter type and implement From<Person> for String. If this is not the case you have to define a new trait and implement the logic for every struct you will use with run.
Well, just what are you trying to do? Why do you think you need your function to accept multiple types?
The whole point of traits is that they specify what you can do with a generic type. What common capabilities does your function need in order to work? If there are none, e.g. it just moves stuff around in memory, you probably don't need any trait bounds. But that's rare.
It's also possible that you always want a specific type, in which case I don't understand the question.
the way i want to design is, my main.rs uses sql client, so i separated as a module i.e sql_client.rs
now you all helped me understand that i have to use trait and impl on all the data type i want to pass it to sql module.
One last question before i mark this a close.
if define a struct in main.rs and pass it to sql module, then i get error saying struct is not in the scope.
i know how to call exported modules in main, but how can i use the struct use a struct in sql_client.rs which is defined in main.rs.
reason i separated is , my module takes two parameters, one is sql query as string and struct. because query and struct may vary.
Your struct seems logically relevant to the functions in sql_client.rs module, I think you should define it there. You can easly import it in the main.rs and use.
I am able to define Struct in main.rs and use crate::Struct in sql.rs?
it's possible and worked for me, i'm not sure if it's right.
i agree with you but my let me explain the problem. sql_client module takes query string, so im using 10 different queries, have to add new struct to sql_client module each time i write new query in main.rs.
I don't really understand. You are saying that functions in sql_client takes a String as parameter and you have to create a new struct for every query. Why? Can you share the code?
Then you should put structs and queies in the same module.
Please read the Forum Code Formatting and Syntax Highlighting that is pinned to the top of this forum, then edit all of your prior posts that contain code by using the edit button under each of those posts.
Many readers of this forum will ignore code snippets that do not follow those posting guidelines. Even those who do respond may feel that the lack of following the forum posting guidelines is disrespectful of their time. Thanks.