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.
Is there an equivalent of this in Rust?
Example of __func__
in C:
#include "stdio.h"
void funny_hello() {
printf ("Hello from %s\n", __func__);
}
int main() {
funny_hello();
}
Outputs Hello from funny_hello
.
There was an RFC about this, but it was never agreed upon or implemented.
The rationale for its absence:
Maybe Rust will have something comparable in the future,
but for now you will need to rely on your own tagging.
side note:
__FUNCTION__
is non standard,__func__
exists in C99 / C++11.You can hack one together with
std::any::type_name
.Note that this gives a full pathname, so
my::path::my_func
instead of justmy_func
. A demo is available.