Reputation: 53
So my question is whether this is possible, and if it is, how would one implement this. For example,
fn some_fn(){}
let name = get_name_of(some_fn);
println!({}, name);
Which outputs
some_fn
Would an implementation like this be possible?
Upvotes: 4
Views: 3495
Reputation: 5545
No, this currently does not seem to be possible as of 1.44
.
Depending on your use case, you could of course still manually specify the name using macros such as println!
or format the given function using stringify!
.
For example, the following will print fn: some_fn
:
fn main() {
println!("fn: {}", std::stringify!(some_fn));
}
fn some_fn() {}
The downside is that it simply prints the given argument and does not allow you to get the actual function name. Here's an example where it fails, as fn: f
and not fn: some_fn
will be returned:
fn main() {
some_fn_as_param(&some_fn);
}
fn some_fn() {}
fn some_fn_as_param(f: &dyn Fn()) {
println!("fn: {}", stringify!(f));
}
As mentioned in the comments, talk of such a macro
for rust
can be tracked here:
Upvotes: 6