Equivalent of __func__ or __FUNCTION__ in Rust?

前端 未结 3 1158
独厮守ぢ
独厮守ぢ 2021-02-07 02:37

In C and C++ you can get the name of the currently executing function through the __func__ macro with C99 & C++11 and ___FUNCTION___ for MSVC.

3条回答
  •  孤独总比滥情好
    2021-02-07 03:13

    You can hack one together with std::any::type_name.

    macro_rules! function {
        () => {{
            fn f() {}
            fn type_name_of(_: T) -> &'static str {
                std::any::type_name::()
            }
            let name = type_name_of(f);
            &name[..name.len() - 3]
        }}
    }
    

    Note that this gives a full pathname, so my::path::my_func instead of just my_func. A demo is available.

提交回复
热议问题