C ___func___ in Rust 1.75?

Equivalent of func or FUNCTION in Rust?

Good people,

is this Stack Overflow discussion thread (7 years old) still current and still relevant in Rust 1.75 ?

Which is to ask - is there Rust built-in support for fetching a function name in a manner similar to line!(), column!(), file!(), module_path!() ??

#include "stdio.h"

void funny_hello() {
    printf ("Hello from %s\n", __func__);
}

int main() {
    funny_hello();
}

thank you.

I don't think there is built-in support. See replies to Does function know its name?

1 Like

Thank you, I feared this when I posted here but still wanted to confirm.

I realise that run-time type reflection is really for Java/.Net/Python-style languages.

I figured at the compile time Rust compiler knows the actual name of the function where a macro function_name!() call would be located (even if it may be in-lined by compiler for execution).

I am not looking for a perfect solution here, just a good-enough one.
This macro would be used only for tracing, logging and debugging use cases, just like line!(), column!(), etc macros.

I also realise there may be some other hacks/solutions/crates to get this done, as mentioned, but I was rather hoping for an official macro to get the function name obtained.

BTW it does not need to be a macro either, any other compile-time-constant-value would do.

thanks for reading thus far.

How about Location::caller?

4 Likes

thanks, I just tried it,
I did not get the function name, just got source file name, line, column information, which is what I can get via macros in standard Rust 1.75

// this is inside actual fn() body
     let l = Location::caller();
    println!("where_am_i l:{l}");

is this what I should have tried?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.