ADVERTISEMENT
ADVERTISEMENT

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 »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.