某些枚举变体未实现的调试特征

时间:2018-12-26 05:01:08

标签: rust

我有这个枚举:

#[derive(Debug)]
pub enum TokenType {
    Illegal,
    Integer(String),
    Ident(String),
}

fn main() {
    let vals = vec![(TokenType::Ident, "identifier")];
    println!("Expected one of {:?}", vals);
}

Playground

当我尝试使用TokenType值时,它似乎忽略了Debug派生,并且出现以下编译器错误:

error[E0277]: `fn(std::string::String) -> TokenType {TokenType::Ident}` doesn't implement `std::fmt::Debug`
  --> src/main.rs:10:38
   |
10 |     println!("Expected one of {:?}", vals);
   |                                      ^^^^ `fn(std::string::String) -> TokenType {TokenType::Ident}` cannot be formatted using `{:?}` because it doesn't implement `std::fmt::Debug`
   |
   = help: the trait `std::fmt::Debug` is not implemented for `fn(std::string::String) -> TokenType {TokenType::Ident}`
   = note: required because of the requirements on the impl of `std::fmt::Debug` for `(fn(std::string::String) -> TokenType {TokenType::Ident}, &str)`
   = note: required because of the requirements on the impl of `std::fmt::Debug` for `std::vec::Vec<(fn(std::string::String) -> TokenType {TokenType::Ident}, &str)>`
   = note: required by `std::fmt::Debug::fmt`

在我看来,这个问题是因为我有一个包含String(例如Ident(String))的枚举的一些变体,它没有正确地推导Debug特性,但我不知道如何解决。

是否有某种方法可以强制Rust为该枚举派生特征,或者有一种方法可以通过我手动为这些变体实现fmt::Debug来解决此错误?

1 个答案:

答案 0 :(得分:2)

TokenType::Ident不是枚举变量;它是一个枚举变量 constructor 。错误消息指出它是一个函数:

fn(std::string::String) -> TokenType {TokenType::Ident}

函数未实现Debug。无法满足您的需求。