Equivalent of __func__ or __FUNCTION__ in Rust?

前端 未结 3 1162
独厮守ぢ
独厮守ぢ 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:15

    Adding to Veedrac's answer, you can get the function's name without its full path by adding this:

    macro_rules! function {
        () => {{
            fn f() {}
            fn type_name_of(_: T) -> &'static str {
                std::any::type_name::()
            }
            let name = type_name_of(f);
    
            // Find and cut the rest of the path
            match &name[..name.len() - 3].rfind(':') {
                Some(pos) => &name[pos + 1..name.len() - 3],
                None => &name[..name.len() - 3],
            }
        }};
    }
    

    You will get my_func instead of my::path::my_func for example.

提交回复
热议问题