# How can I print the type of a variable?

**URL:** <https://users.rust-lang.org/t/how-can-i-print-the-type-of-a-variable/4183>\
**Category:** help\
**Created:** [January 6, 2016, 6:29am UTC](https://users.rust-lang.org/t/how-can-i-print-the-type-of-a-variable/4183 "2016-01-06T06:29:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![wxwhaha](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/wxwhaha/32/1774_2.png) [@wxwhaha](https://users.rust-lang.org/u/wxwhaha)\
**Post date:** [January 6, 2016, 6:29am UTC](https://users.rust-lang.org/t/how-can-i-print-the-type-of-a-variable/4183/1 "2016-01-06T06:29:53Z")

</div>

```rust
fn main() {
    let x = 3;
    let y = 2.3;
    //how to print types of x, y.
    //i32, f32..
    println!("");
}

```

---

<div class="post-metadata">

**Author:** ![DanielKeep](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/danielkeep/32/1713_2.png) [@DanielKeep](https://users.rust-lang.org/u/DanielKeep)\
**Post date:** [January 6, 2016, 7:11am UTC](https://users.rust-lang.org/t/how-can-i-print-the-type-of-a-variable/4183/2 "2016-01-06T07:11:43Z")

</div>

You _don't_.

The closest would be a type's `TypeId`, ~~but even then you need to already know what the type is,~~ and it looks like this:

```rust
fn main() {
    println!("{:?}", std::any::TypeId::of::<i32>());
}

```

```nohighlight
TypeId { t: 10645063183773766558 }

```

If what you're _actually_ trying to do is fine out what the type of an expression is, you can always ask the compiler by introducing a deliberate type mismatch:

```rust
fn main() {
    let _: () = 42;
}

```

```nohighlight
<anon>:2:17: 2:19 error: mismatched types:
 expected `()`,
    found `_`
(expected (),
    found integral variable) [E0308]
<anon>:2 let _: () = 42;
                         ^~
<anon>:2:17: 2:19 help: see the detailed explanation for E0308

```

---

<div class="post-metadata">

**Author:** ![robson](https://avatars.discourse-cdn.com/v4/letter/r/8797f3/32.png) [@robson](https://users.rust-lang.org/u/robson)\
**Post date:** [January 6, 2016, 7:42am UTC](https://users.rust-lang.org/t/how-can-i-print-the-type-of-a-variable/4183/3 "2016-01-06T07:42:46Z")

</div>

You can use TypeId without knowing the type:

```
fn typeid<T: std::any::Any>(_: &T) {
    println!("{:?}", std::any::TypeId::of::<T>());
}

struct Foo;
struct Bar;

fn main() {
    let f = Foo;
    typeid(&f);
    let b = Bar;
    typeid(&b);
    
    let x = 5;
    typeid(&x);
    println!("{:?}", std::any::TypeId::of::<i32>());
    
    let y = 5.;
    typeid(&y);
    println!("{:?}", std::any::TypeId::of::<f64>());
}

```

---

<div class="post-metadata">

**Author:** ![wxwhaha](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/wxwhaha/32/1774_2.png) [@wxwhaha](https://users.rust-lang.org/u/wxwhaha)\
**Post date:** [January 6, 2016, 7:51am UTC](https://users.rust-lang.org/t/how-can-i-print-the-type-of-a-variable/4183/4 "2016-01-06T07:51:59Z")

</div>

Thanks for help!

---

<div class="post-metadata">

**Author:** ![mww](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mww/32/13780_2.png) [@mww](https://users.rust-lang.org/u/mww)\
**Post date:** [November 24, 2019, 11:51pm UTC](https://users.rust-lang.org/t/how-can-i-print-the-type-of-a-variable/4183/5 "2019-11-24T23:51:30Z")

</div>

There are perfectly legitimate reasons to want to print types. One I'm working with right now is a case where I had to erase the type on a pointer and place the TypeId next to it so I can perform a checked dynamic cast later. If I've made an error in performing this checked cast, I'd like to be able to indicate the before and after type to help me debug this information after the fact.

---

<div class="post-metadata">

**Author:** ![cbiffle](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/cbiffle/32/841_2.png) [@cbiffle](https://users.rust-lang.org/u/cbiffle)\
**Post date:** [November 25, 2019, 12:03am UTC](https://users.rust-lang.org/t/how-can-i-print-the-type-of-a-variable/4183/6 "2019-11-25T00:03:08Z")

</div>

Yeah, a slightly more helpful way to answer the question might be...

Printing the name of a type _at runtime_ is complicated in Rust, largely because types don't really exist at runtime. In a language like Java, objects contain class pointers that indicate their type, and you can often reach the type's name through them (`o.getClass().getName()` if I remember my Java correctly). Rust doesn't include type information in every value, so that approach is right out.

There are some alternatives, but none is perfect.

If you're in a context where you "know" the type -- say, a generic function -- there is an [unstable intrinsic `type_name::<T>()`](https://doc.rust-lang.org/std/intrinsics/fn.type_name.html). The "unstable" part means you're stuck on the nightly compiler, which may or may not work for you. ( **Edit:** see @leudz's post below, it has been re-exported as a stable API when I wasn't looking!)

In your case, it sounds like you're doing something much like `Any`, where you store a typeid and check equality later to implement a protected downcast. One of the ergonomic issues with `Any` is that, if the downcast fails, _it can't explain why or print the type name,_ for the same reason you're having trouble. There doesn't appear to be a way to go from a typeid to a string name right now. At the cost of an additional pointer, you could capture the name (perhaps using that unstable intrinsic) when the concrete type is being erased, and so you wind up storing `(typeid, &str, T)`.

There's also the [`typename` crate](https://docs.rs/typename/0.1.1/typename/), which is an alternative to the intrinsic. It derives a trait for types whose names can be obtained. However, you have to impl that trait for any type you want to talk about, which seems limiting. Plus, in a type-erased context, you need to be holding a `&dyn TypeName` to get the right vtable... not a `&dyn Any` that would let you downcast. (Though I suppose you could ask `TypeName` when you erase the type and store the result, just like I suggested above.) Also, `TypeName` allocates (it returns a `String` rather than a `&'static str`), and while I understand _why_, allocations still make me sad, and I bet you don't want to incur a string allocation every time a type gets erased. (But I could be wrong!)

Hope that helps.

---

<div class="post-metadata">

**Author:** ![leudz](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/leudz/32/10514_2.png) [@leudz](https://users.rust-lang.org/u/leudz)\
**Post date:** [November 25, 2019, 12:18am UTC](https://users.rust-lang.org/t/how-can-i-print-the-type-of-a-variable/4183/7 "2019-11-25T00:18:12Z")

</div>

The intrinsic is unstable but there's a stable "re-export" since 1.38: [`any::type_name`](https://doc.rust-lang.org/core/any/fn.type_name.html).

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/steffahn/32/47569_2.png) [@steffahn](https://users.rust-lang.org/u/steffahn)\
**Post date:** [July 3, 2022, 1:55pm UTC](https://users.rust-lang.org/t/how-can-i-print-the-type-of-a-variable/4183/8 "2022-07-03T13:55:19Z")

</div>

Closed on July 3, 2022, 1:55pm UTC


