Named functions don't implement Debug

6144cce
Opened by Alan Jeffrey at 2024-12-21 05:10:29

Trying to compile (https://play.rust-lang.org/?gist=3b38f384f0f7ddecc142&version=stable):

fn foo() {}
fn main() {
    println!("{:?}", (foo as fn())); // Works
    println!("{:?}", foo);   // Fails
} 

It seem that named functions have to be coerced to anonymous ones in order to implement Debug. This means any structs containing functions don't derive Debug, which makes unit testing tricky.

  1. I guess it would have to be an impl-by-compiler-magic since they are all separate types. Would you expect it to print out the memory address like the casted version (which, as I understand it, may not even exist due to inlining etc) or the function name?

    Alex Burka at 2016-02-10 01:54:51

  2. The function name would be best, even just "..." would be fine, currently we can't use unit tests with assert_eq with any type which contains a named function.

    Alan Jeffrey at 2016-02-10 03:54:04

  3. Triage: this is still true today.

    Steve Klabnik at 2019-03-16 14:58:56