Macro nameof::name_of [] [src]

macro_rules! name_of {
    ($n: ident) => { ... };
    (type $t: ty) => { ... };
    ($n: ident for $t: ty) => { ... };
}

Takes a binding, type, 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!().

Examples

// Bindings
let text = "Hello, World!";
println!("Variable `{}` holds `{}`.", name_of!(text), text);

// Functions
fn greet() { }
println!("Function is called `{}`.", name_of!(greet));

// Types & Fields
struct TestStruct { test_field: i32 }
println!("Struct is called `{}`.", name_of!(type TestStruct));
println!("Standard Types: `{}`.", name_of!(type i32));
println!("Field is called `{}`.", name_of!(test_field for TestStruct));