具有引用参数的函数指针无法派生Debug

时间:2018-11-19 17:44:05

标签: rust

我有一个要归纳函数指针的枚举。我在函数指针定义中添加了一个引用。无法编译,因为它无法使用Debug打印它:

fn div1(t: i64, b: i64) -> i64 {
    t / b
}

fn div2(t: i64, b: &i64) -> i64 {
    t / b
}

#[derive(Debug)]
enum Enum {
    FnTest1(fn(i64, i64) -> i64),
    FnTest2(fn(i64, &i64) -> i64),
}

fn main() {
    println!("{:?}", Enum::FnTest1(div1));
    println!("{:?}", Enum::FnTest2(div2));
}

我得到的错误是这个

error[E0277]: `for<'r> fn(i64, &'r i64) -> i64` doesn't implement `std::fmt::Debug`
  --> src/main.rs:12:13
   |
12 |     FnTest2(fn(i64, &i64) -> i64),
   |             ^^^^^^^^^^^^^^^^^^^^ `for<'r> fn(i64, &'r i64) -> i64` cannot be formatted using `{:?}` because it doesn't implement `std::fmt::Debug`
   |
   = help: the trait `std::fmt::Debug` is not implemented for `for<'r> fn(i64, &'r i64) -> i64`
   = note: required because of the requirements on the impl of `std::fmt::Debug` for `&for<'r> fn(i64, &'r i64) -> i64`
   = note: required for the cast to the object type `dyn std::fmt::Debug`

仅显示FnTest2的错误,该错误具有一个引用参数,而FnTest1正常工作。

这是Rust中的错误,还是此问题的解决方案或替代方法?

我每晚都在运行Rust(rustup说:rustc 1.30.0-nightly (ae7fe84e8 2018-09-26))。