How to fix "doesn't implement `std::fmt::Debug`"

use std::collections::HashMap;
fn main() {
    let mut object = HashMap::new();
    let m = String::from("string");
    object.insert(m, mmm);
    let c = String::from("string");
    let mut xm = object.get(&c);
    // it can't work 
    println!("{:?} unwraps", xm);
}
fn mmm() {
    println!("dsdsds")
}

error : ^^ fn() {mmm} cannot be formatted using {:?} because it doesn't implement Debug
= help: the trait Debug is not implemented for fn() {mmm}
= help: use parentheses to call the function: mmm()
= note: required because of the requirements on the impl of Debug for &fn() {mmm}
= note: 1 redundant requirements hidden
= note: required because of the requirements on the impl of Debug for Option<&fn() {mmm}>
= note: required by std::fmt::Debug::fmt
= note: this error originates in the macro $crate::format_args_nl (in Nightly builds, run with -Z macro-backtrace for more info)

i want to type of option,but it wasn't work

Blockquote

In this particular case, you cannot.

why ? .i only want type "xm"

Suppose it worked, what output are you expecting ?

Why? The formal reason of the "why" is exactly as you have seen in the error message.
The informal reason is that the type of the function pointer doesn't store the name of the function. That is why it cannot be printed.

1 Like

it can't work,
my expectation this one of println!("{:?} unwraps", xm) can run

I am asking you what output you expect so we can help you modify the code to generate said output.

it output mmm this funciton

Rust does not store the name of the function in a function pointer.

2 Likes

so how can i type of function name of "mmm"?
could you help me ,please

in js.

function mmm (){}
let object = {string: mmm}
console.log(object.mmm)

this one , i can type this mmm in object in js.
so i want to try in rust

With the information you're storing (that is, just the function pointer), it is not possible. You'd need to write a macro which separately stores the function name.

2 Likes

Yes, so this is not JS, right?
So things that work in JS don't work the same way in Rust. You'd have to work your way around them.

1 Like

If you just want to see where you are, I think the dbg! macro prints the file and line number (in addition to anything you pass to the macro).

1 Like
object.insert(m, ("mmm", mmm))
...
println!("{:?} unwraps", xm.0)

should work

i just learn rust.
thank you so mush

I apologize ,
I don't understand .
could you write code completely ?

could you tell me how to make it work in rust
how to achieve "You'd need to write a macro which separately stores the function name." in rust ?
please thank you

One way would be something like this:

use core::fmt::{Debug, Formatter};
use std::ops::{Deref,DerefMut};

struct Annotated<T:?Sized> {
    dbg: &'static str,
    obj: T
}

impl<T:?Sized> Debug for Annotated<T> {
    fn fmt(&self, format: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
        self.dbg.fmt(format)
    }
}

impl<T:?Sized> Deref for Annotated<T> {
    type Target = T;
    fn deref(&self)->&T { &self.obj }
}

impl<T:?Sized> DerefMut for Annotated<T> {
    fn deref_mut(&mut self)->&mut T { &mut self.obj }
}

macro_rules! annotate {
    { $e:expr } => { Annotated { dbg: stringify!(Annotated { $e }), obj: $e }}
}

fn mmm() {
    println!("Called mmm");
}

fn main() {
    let m = annotate!(mmm);
    m();
    println!("{:?}", m);
}
2 Likes

thank you