The source code to print the type of variables is given below. The given program is compiled and executed successfully.
// Rust program to print the type of variables
fn TypeOf<T>(_: &T) {
println!("{}", std::any::type_name::<T>())
}
fn main() {
let s = "Hello";
let n1 = 42;
let n2 = 42.68;
let b = true;
TypeOf(&s);
TypeOf(&n1);
TypeOf(&n2);
TypeOf(&b);
}
Output:
&str
i32
f64
bool
Explanation:
Here, we created 4 variables of different types. Then we printed the datatype of all variables using the TypeOf() function.
Program/Source Code:
The source code to print the type of variables is given below. The given program is compiled and executed successfully.
Output:
Explanation:
Here, we created 4 variables of different types. Then we printed the datatype of all variables using the TypeOf() function.
need an explanation for this answer? contact us directly to get an explanation for this answer