#name #macro #string #const #determine #bindings #name-of

ident-util

Provides a Rust macro to determine the string name of a binding, type, const, or function

2 stable releases

Uses old Rust 2015

1.0.1 Mar 6, 2022
1.0.0 Sep 24, 2020

#1295 in Rust patterns

MIT license

11KB
125 lines

ident-util

Crate Version MIT License

The name_of!() macro defined in this crate takes a binding, type, const, or function as an argument and returns its unqualified string representation. If the identifier does not exist in the current context, the macro will cause a compilation error. This macro is mainly intended for debugging purposes and to improve the refactoring experience compared to stringify!().

Usage

Add ident-util as a dependency to your project's Cargo.toml file:

[dependencies]
ident-util = "1"

To use the macro(s), import the crate with the required annotation:

#[macro_use]
extern crate ident_util;

fn main() {
    let text = "Hello, World!";
    println!("Binding `{}` holds `{}`.", name_of!(text), text);
}

Examples

The name_of!() macro is used as follows:

#[macro_use]
extern crate ident_util;

struct TestStruct {
    test_field: i32,
}

impl TestStruct {
    const TEST_CONST: i32 = 1;
}

struct GenericStruct<T> {
    test_field_t: T,
}

fn greet() -> &'static str {
    "Hi, World"
}

fn main() {
    let text = "Hello, World!";

    println!("Binding `{}` holds `{}`.", name_of!(text), text);

    println!("Function `{}` says `{}`.", name_of!(greet), greet());

    println!(
        "Struct `{}` has a field `{}`.",
        name_of!(type TestStruct),
        name_of!(test_field in TestStruct)
    );

    println!(
        "Generic Struct `{}` has a field `{}`.",
        name_of!(type GenericStruct<String>),
        name_of!(test_field_t in GenericStruct<String>)
    );

    println!(
        "Struct `{}` has an associated constant `{}`.",
        name_of!(type TestStruct),
        name_of!(const TEST_CONST in TestStruct)
    );

    println!(
        "Standard types such as `{}` and `{}` also work.",
        name_of!(type i32),
        name_of!(type f64)
    );

    println!("{}", path_of!(crate::greet));
}

Alternatively, name_of_type!(T) can be used instead of name_of!(type T).

#[macro_use]
extern crate ident_util;

struct TestStruct {
    test_field: i32,
}

fn main() {
    println!("Struct is called `{}`.", name_of_type!(TestStruct));
    println!("Type is called `{}`.", name_of_type!(i32));
}

License

See LICENSE.txt.

No runtime deps