# Thoughts about \`type\_name\` for debugging

**URL:** <https://internals.rust-lang.org/t/thoughts-about-type-name-for-debugging/21641>\
**Category:** libs\
**Created:** [October 1, 2024, 6:50pm UTC](https://internals.rust-lang.org/t/thoughts-about-type-name-for-debugging/21641 "2024-10-01T18:50:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![stackinspector](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/stackinspector/32/9487_2.png) [@stackinspector](https://internals.rust-lang.org/u/stackinspector)\
**Post date:** [October 1, 2024, 6:50pm UTC](https://internals.rust-lang.org/t/thoughts-about-type-name-for-debugging/21641/1 "2024-10-01T18:50:44Z")

</div>

I found myself implementing this helper trait

```rust
trait TypeNameOf {
    fn type_name(&self) -> &'static str {
        core::any::type_name::<Self>()
    }
}

impl<T> TypeNameOf for T {}

```

and then

```rust
dbg!(some_value.type_name());

```

for debugging because

```rust
dbg!(core::any::type_name_of_val(&some_value));

```

is a little bit ugly.

I can think of 3 approaches to achieve "make it convenient to know types for debugging purposes":

1. Impl the above `TypeNameOf` in `core::any` (replace the `type_name` call with `intrinsics` one of course).
2. Impl `fn type_name(&self) -> &'static str` in `core::any::Any`. I won't recommend this, though it seems reasonable on the surface: what about types that are not `'static`?
3. Impl a declarative macro named `dbg_typeof!` in `std` with similar idea (and modified code) with `dbg!`:

```rust
macro_rules! dbg_typeof {
    ($val:expr $(,)?) => {
        // Use of `match` here is intentional because it affects the lifetimes
        // of temporaries - https://stackoverflow.com/a/48732525/1063961
        match &$val {
            tmp => {
                $crate::eprintln!("[{}:{}:{}] typeof({}): {}",
                    $crate::file!(), $crate::line!(), $crate::column!(), $crate::stringify!($val), core::any::type_name_of_val(tmp));
            }
        }
    };
    ($($val:expr),+ $(,)?) => {
        ($($crate::dbg!($val)),+,)
    };
}

```

(The form of the output message can be adjusted)

This is what I preferred because it emphasizes that `type_name` is "debug only" and should not be relied upon.

---

<div class="post-metadata">

**Author:** ![binarycat](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/binarycat/32/12210_2.png) [@binarycat](https://internals.rust-lang.org/u/binarycat)\
**Post date:** [October 2, 2024, 7:23am UTC](https://internals.rust-lang.org/t/thoughts-about-type-name-for-debugging/21641/2 "2024-10-02T07:23:22Z")

</div>

what are you doing that you need to learn the name of a type at _runtime_?

if i need to know the type of an expression, i usually just do `let _: () = expr` and read the compile error.

---

<div class="post-metadata">

**Author:** ![stackinspector](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/stackinspector/32/9487_2.png) [@stackinspector](https://internals.rust-lang.org/u/stackinspector)\
**Post date:** [October 2, 2024, 6:42pm UTC](https://internals.rust-lang.org/t/thoughts-about-type-name-for-debugging/21641/3 "2024-10-02T18:42:06Z")

</div>

For example when testing performance and memory fingerprint difference in a particular scenario between `HashMap` and `BTreeMap` and maybe another data structure, you want to distinguish which data structure are the 2 or more printed test results belong to.

---

<div class="post-metadata">

**Author:** ![kpreid](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kpreid/32/8484_2.png) [@kpreid](https://internals.rust-lang.org/u/kpreid)\
**Post date:** [October 2, 2024, 7:21pm UTC](https://internals.rust-lang.org/t/thoughts-about-type-name-for-debugging/21641/4 "2024-10-02T19:21:13Z")

</div>

I'm surprised that that comes up enough to be worth any extra setup. I'd think that that would be handled practically enough by giving explicit labels to the cases, which also lets you name non-type-related parts of the test like changes in the size or composition of the test data set.

```rust
fn benchmarks() {
    my_map_bench::<BTreeMap<String, f32>>("btree");
    my_map_bench::<HashMap<String, f32>>("hash");
}

```

Do you write a lot of benchmarks or tests that are solely distinguished by type?

---

<div class="post-metadata">

**Author:** ![cdbennett](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cdbennett/32/12055_2.png) [@cdbennett](https://internals.rust-lang.org/u/cdbennett)\
**Post date:** [October 3, 2024, 10:05pm UTC](https://internals.rust-lang.org/t/thoughts-about-type-name-for-debugging/21641/5 "2024-10-03T22:05:27Z")

</div>

> [@stackinspector](#):
>
> ... then
> 
> ```rust
> dbg!(some_value.type_name());
> 
> ```
> 
> for debugging because
> 
> ```rust
> dbg!(core::any::type_name_of_val(&some_value));
> 
> ```
> 
> is a little bit ugly.

The second expression isn't all that bad, is it? You could even `use core::any::type_name_of_val` and then it's super clean:

```rust
use core::any::type_name_of_val as type_name;
...
fn foo() {
    ...
    dbg!(type_name(&some_value));
}

```

Seems like a free function is just as good as an associated function? And better because it doesn't require adding anything extra?

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [March 27, 2026, 10:05pm UTC](https://internals.rust-lang.org/t/thoughts-about-type-name-for-debugging/21641/6 "2026-03-27T22:05:44Z")

</div>

Closed on March 27, 2026, 10:05pm UTC

This topic was automatically closed 540 days after the last reply. New replies are no longer allowed.
