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)
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.
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.
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