Q:

Rust program to print the type of variables

belongs to collection: Rust Basic Programs

0

Here, we will print the datatype of created variables and print the result.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Rust Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Rust program to calculate the area of the circle... >>
<< Rust program to find the 2\'s complement of t...