Equivalent of __func__ or __FUNCTION__ in Rust?

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

macro_rules! function {
    () => {{
        fn f() {}
        fn type_name_of<T>(_: T) -> &'static str {
            std::any::type_name::<T>()
        }
        let name = type_name_of(f);
        name.strip_suffix("::f").unwrap()
    }}
}

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)