How check type of variable

let x = 21;
let y = 2.5;

type_of(x) //i32

Newly added in version 1.38 std::any::type_name!!

use std::any::type_name;

fn type_of<T>(_: T) -> &'static str {
    type_name::<T>()
}
fn main() {
    let x = 21;
    let y = 2.5;
    println!("{}", type_of(&y));
    println!("{}", type_of(x));
}

playground

Note the warning in the docs, however:

This is intended for diagnostic use. The exact contents and format of the string are not specified, other than being a best-effort description of the type. For example, type_name::<Option<String>>() could return the "Option<String>" or "std::option::Option<std::string::String>" , but not "foobar" . In addition, the output may change between versions of the compiler.

So you should not rely too heavily on the output of this function.

1 Like

Although the concrete answer was already done, I'd try to explain a bit more, since, looking at this and earlier posts, there might be a critical misunderstanding of the language.

You're asking, how to check what type the variable has. OK. But what will you do with this information?

In dynamically-typed languages, as well as in languages like TypeScript (which are dynamically-typed at runtime) or Java (which allow relatively free up- and downcasting), this is a rather common thing: given a value of some type (it might be something like unknown or Object, i.e. the type without any detailed information at all), and we'd like to check if this value is in fact of another type (which has the desired behaviour). Looking for your previous questions, it looks like you're used to this kind of languages, where you have to perform at least some typechecking manually.

But Rust, like many other languages with strong static typing, does this checks itself at compile-time. Well, you still can somewhat mimic "dynamic typechecking" by using Any trait and asking the value at runtime whether it is of certain type or not, but this is extremely niche case.
Usually, you don't want to check the types. You want to provide the types which will be checked by the compiler.

4 Likes
1 Like

Also, you can use an IDE for this. A free solution that works exceptionally well is VSCode + rust-analyzer.

  • on hover (+ grey type annotations that are automagically shown by the plugin):

    image

To get it, you just have to:

  1. Get VSCode;

  2. Clone and install rust-analyzer:

    1. git clone https://github.com/rust-analyzer/rust-analyzer

    2. cd rust-analyzer && cargo xtask install

And you are good to go.

1 Like
use std::any::Any;

pub trait Object {
    fn type_name(&self) -> &str;
    fn as_any(&self) -> &dyn Any;
}

pub fn type_name(x: &dyn Object) -> &str {
    x.type_name()
}

pub fn is_of_type<T: 'static>(x: &dyn Object) -> bool {
    x.as_any().is::<T>()
}

impl Object for i32 {
    fn type_name(&self) -> &str {"i32"}
    fn as_any(&self) -> &dyn Any {self}
}

fn main() {
    let x = 21;
    println!("{}",type_name(&x));
    println!("{}",is_of_type::<i32>(&x));
}