# Implement alternative to FUNCNAME

**URL:** <https://internals.rust-lang.org/t/implement-alternative-to-funcname/13891>\
**Category:** compiler\
**Created:** [January 23, 2021, 6:16pm UTC](https://internals.rust-lang.org/t/implement-alternative-to-funcname/13891 "2021-01-23T18:16:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kreyren](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kreyren/32/7668_2.png) [@Kreyren](https://internals.rust-lang.org/u/Kreyren)\
**Post date:** [January 23, 2021, 6:16pm UTC](https://internals.rust-lang.org/t/implement-alternative-to-funcname/13891/1 "2021-01-23T18:16:12Z")

</div>

```rust
#!/bin/bash

crab() {
  echo "$FUNCNAME" # Returns 'crab'
}

```

Proposing similar implementation in rustlang

### Rationale

1. Better debugging: This would allow custom debugging channels to implement messages such as: `Function 'crab' failed to do A B C, because of 1 2 3`

2. Use function names in a logic

```rust
fn thing() {
  match functionName == "thing" {
    true => println!("Called from function called 'thing'"), 
    _ => println!("This function is not called 'thing' it's called {:1:}", functionName)
  }
}

```

* * *

NOTE: There are a ways in rustlang to get this result, but that makes the code look ugly as it requires mentioning the function name within the function.

---

<div class="post-metadata">

**Author:** ![mbrubeck](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mbrubeck/32/174_2.png) [@mbrubeck](https://internals.rust-lang.org/u/mbrubeck)\
**Post date:** [January 23, 2021, 6:22pm UTC](https://internals.rust-lang.org/t/implement-alternative-to-funcname/13891/2 "2021-01-23T18:22:50Z")

</div>

Some past discussion of this: [https://github.com/rust-lang/rfcs/issues/1743](https://github.com/rust-lang/rfcs/issues/1743)

---

<div class="post-metadata">

**Author:** ![chrisd](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/chrisd/32/7232_2.png) [@chrisd](https://internals.rust-lang.org/u/chrisd)\
**Post date:** [January 23, 2021, 6:44pm UTC](https://internals.rust-lang.org/t/implement-alternative-to-funcname/13891/3 "2021-01-23T18:44:52Z")

</div>

`stdext` has a macro that does this: [stdext::function\_name](https://docs.rs/stdext/0.2.1/stdext/macro.function_name.html). It's a bit of a hack but possibly good enough for `println` debugging. It uses [`std::any::type_name`](https://doc.rust-lang.org/std/any/fn.type_name.html).

Also tools can map [`file!`](https://doc.rust-lang.org/std/macro.file.html), [`line!`](https://doc.rust-lang.org/std/macro.line.html) and [`column!`](https://doc.rust-lang.org/std/macro.column.html) to function names.

That said, it would be good to have a macro for this to compliment [`module_path!`](https://doc.rust-lang.org/std/macro.module_path.html).

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/josh/32/5934_2.png) [@josh](https://internals.rust-lang.org/u/josh)\
**Post date:** [January 23, 2021, 7:51pm UTC](https://internals.rust-lang.org/t/implement-alternative-to-funcname/13891/4 "2021-01-23T19:51:28Z")

</div>

I would love to see this in the standard library. The consensus of the lang team last time it came up was that we all want this.

It seems to have stalled out the last time it was started, but there's an RFC from that effort that could be revived and updated.

---

<div class="post-metadata">

**Author:** ![ckaran](https://avatars.discourse-cdn.com/v4/letter/c/f475e1/32.png) [@ckaran](https://internals.rust-lang.org/u/ckaran)\
**Post date:** [January 25, 2021, 2:44pm UTC](https://internals.rust-lang.org/t/implement-alternative-to-funcname/13891/5 "2021-01-25T14:44:26Z")

</div>

> [@chrisd](#):
>
> Also tools can map [`file!`](https://doc.rust-lang.org/std/macro.file.html), [`line!`](https://doc.rust-lang.org/std/macro.line.html) and [`column!`](https://doc.rust-lang.org/std/macro.column.html) to function names.

When you say 'tools', do you mean some kind of macro that wraps them? If so, what macro is it?

Also, IIRC the discussion that @mbrubeck linked to stalled out over what to do about closures (and maybe anything else that is like an anonymous function; do futures count here too? Or the unstable [`call()`](https://doc.rust-lang.org/std/ops/trait.Fn.html#tymethod.call) method?). Maybe instead of just a plain `func!()`, we have `func!(file!(), line!(), column!())` fallback option so that if the inner most function-like scope is anonymous we fall back to something involving [`file!`](https://doc.rust-lang.org/std/macro.file.html), [`line!`](https://doc.rust-lang.org/std/macro.line.html) and [`column!`](https://doc.rust-lang.org/std/macro.column.html) instead. Not great, but since this is just for `println!()` debugging, probably not too horrible either.

Alternatively, once [`std::backtrace`](https://doc.rust-lang.org/std/backtrace/index.html) is stabilized, we could use that as an alternative in those cases where it's unclear what `func!()` should really print out by printing out the full backtrace. Again, not great, but maybe not too horrible either.

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/josh/32/5934_2.png) [@josh](https://internals.rust-lang.org/u/josh)\
**Post date:** [January 25, 2021, 7:10pm UTC](https://internals.rust-lang.org/t/implement-alternative-to-funcname/13891/6 "2021-01-25T19:10:49Z")

</div>

I don't think closures were at issue; closures already have a generated name for other purposes.

The discussion stalled out before transitioning from names to full paths (e.g. `crate::module::func`).

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [April 25, 2021, 7:11pm UTC](https://internals.rust-lang.org/t/implement-alternative-to-funcname/13891/7 "2021-04-25T19:11:45Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
