Rust program to print the type of variables

Define some variables, write a Rust program to print the types of those variables.
Submitted by Nidhi, on September 26, 2021

Problem Solution:

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

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.

Rust Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.