pretty-name
Get the human-friendly name of types, functions, methods, fields, and enum variants in a refactoring-safe way.
Overview
pretty-name provides a set of macros and functions for extracting names of Rust language constructs at compile time. Unlike stringify! or std::any::type_name, this crate offers:
Key Features
-
Human-friendly output: Type names are cleaned to remove module paths (
std::vec::Vec<T>→Vec<T>), lifetime annotations (&'static str→&str), and other visual clutter. -
Refactoring-safe: When you rename items using IDE refactoring tools, the macro calls are automatically updated.
-
Compile-time validation: All macros check that the referenced items exist. If a referenced identifier, field, method, or variant doesn't exist, you get a compile error instead of a runtime panic.
-
Natural syntax: No strange custom syntax like `pretty_name!(type i32).
-
Selfsupport: ResolvesSelfinto the appropriate type.
Installation
Add this to your Cargo.toml:
[]
= "0.1"
Or use cargo add:
Usage
Type Names
Get human-friendly type names with pretty_name::type_name<T>() for types and pretty_name::type_name_of(value) for values:
use ;
// Get type name from a type parameter
assert_eq!;
assert_eq!;
assert_eq!;
// Get type name from a value
let s = "hello";
assert_eq!;
let v = vec!;
assert_eq!;
The output removes module paths (e.g., std::vec::Vec becomes Vec) and lifetime annotations for cleaner, more readable type names.
Identifier Names
Get the name of local variables, constants, and functions with pretty_name::of!:
let my_variable = 42;
assert_eq!;
assert_eq!;
The macro validates that the identifier exists in the current scope. If you rename my_variable using your IDE's refactoring tools, the macro call will be updated automatically.
Struct Field Names
Get the name of struct fields with pretty_name::of_field!:
assert_eq!;
This macro resolves Self when used inside impl blocks and validates that the field exists on the type.
Method Names
Get the name of methods with pretty_name::of_method!:
;
assert_eq!;
The macro resolves Self when used inside impl blocks and validates that the method exists on the type.
Enum Variant Names
Get the name of enum variants with pretty_name::of_variant!. Supports unit, tuple, and struct variants:
// Unit variant
assert_eq!;
// Tuple variant - use (..) syntax
assert_eq!;
// Struct variant - use {..} syntax
assert_eq!;
The macro resolves Self when used inside impl blocks and validates that the variant exists on the enum type.
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.