I'm trying to use function as struct member and encounter a compile error.
the code is:
#[derive(Debug)]
struct D<T> {
func : fn(&T)
}
this will give the error:
main.rs:5:5: 5:18 error: the trait
core::fmt::Debug
is not implemented for the typefn(&T)
[E0277]
main.rs:5 func : fn(&T)
^~~~~~~~~~~~~
main.rs:3:10: 3:15 note: in this expansion of #[derive_Debug] (defined in main.rs)
main.rs:5:5: 5:18 help: runrustc --explain E0277
to see a detailed explanation
main.rs:5:5: 5:18 note:fn(&T)
cannot be formatted using:?
; if it is defined in your crate, add#[derive(Debug)]
or manually implement it
main.rs:5:5: 5:18 note: required for the cast to the object typecore::fmt::Debug
But strangely, when the argument &T
is changed to T
, it will compile.
#[derive(Debug)]
struct D<T> {
func : fn(T)
}
And the func
member will generate a memory address in {:?}
The version of rustc is 1.6.0.
So Why does function with reference argument does not have a default fmt::Debug implementation?
And how can I solve this problem?