#type-name #name #type

tynm

Returns type names in shorter form

12 releases

Uses new Rust 2024

0.2.0 Mar 17, 2025
0.1.10 Feb 12, 2024
0.1.9 Sep 28, 2023
0.1.8 May 26, 2023
0.1.0 Dec 29, 2019

#1 in #type-name

Download history 3124/week @ 2024-12-04 4111/week @ 2024-12-11 2305/week @ 2024-12-18 1228/week @ 2024-12-25 3387/week @ 2025-01-01 3195/week @ 2025-01-08 5449/week @ 2025-01-15 4387/week @ 2025-01-22 4376/week @ 2025-01-29 6909/week @ 2025-02-05 4767/week @ 2025-02-12 4724/week @ 2025-02-19 5548/week @ 2025-02-26 5529/week @ 2025-03-05 13447/week @ 2025-03-12 14117/week @ 2025-03-19

39,334 downloads per month
Used in 114 crates (24 directly)

MIT/Apache

61KB
1K SLoC

🪶 Tynm -- Type Name

Crates.io docs.rs CI Coverage Status

Returns type names with a specifiable number of module segments as a String.

Usage

Add the following to Cargo.toml

tynm = "0.2.0"

In code:

#[rustfmt::skip]
assert_eq!(
    core::any::type_name::<Option<String>>(), "core::option::Option<alloc::string::String>"
);

#[rustfmt::skip]
let tuples = vec![
    (tynm::type_name::<Option<String>>(),    "Option<String>"),
    (tynm::type_namem::<Option<String>>(1),  "core::..::Option<alloc::..::String>"),
    (tynm::type_namen::<Option<String>>(1),  "..::option::Option<..::string::String>"),
    // 1 segment from most and least significant modules.
    (tynm::type_namemn::<rust_out::two::three::Struct>(1, 1), "rust_out::..::three::Struct"),
    // traits
    (tynm::type_name::<dyn core::fmt::Debug>(), "dyn Debug"),
];

tuples
    .iter()
    .for_each(|(left, right)| assert_eq!(left, right));

Motivation

The core::any::type_name function stabilized in Rust 1.38 returns the fully qualified type name with all module segments. This can be difficult to read in error messages, especially for type-parameterized types.

Often, the simple type name is more readable, and enough to distinguish the type referenced in an error.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~0.8–1MB
~20K SLoC