Create a generic function
Rust Programming Language
Problem
In this program, we will create a generic function that can accept any type of parameter.
Input
// Rust program to create a generic functionuse std::fmt::Display;fn genric_fun<T:Display>(n1:T,n2:T){println!("Number1: {}",n1);println!("Number2: {}",n2);}fn main(){genric_fun(10 as u32, 20 as u32);genric_fun(10.23 as f32, 20.56 as f32);}{codeBox}
Output
Number1: 10Number2: 20Number1: 10.23Number2: 20.56{codeBox}
Explanation
In the above program, we created a generic function that can accept any type of parameter. The generic function is given below,
fn genric_fun<T:Display>(n1:T,n2:T){println!("Number1: {}",n1);println!("Number2: {}",n2);}{codeBox}
In the main() function, we called the genric_fun() function with different types of parameters and printed them.