Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug trait not implemented for some enum variants

Tags:

rust

I have this enum:

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

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

Playground

When I try to use a TokenType value, it appears to be ignoring the Debug derivation, and I get the following compiler error:

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`

It would appear to me that the issue is because I have a few variants of that enum that contain a String (ex. Ident(String)) it's not properly deriving the Debug trait, but I don't know how to resolve it.

Is there some way to force Rust to derive the trait for that enum or is there a way that I can resolve this error by manually implementing fmt::Debug for those variants?

like image 590
RPiAwesomeness Avatar asked Sep 19 '25 14:09

RPiAwesomeness


1 Answers

TokenType::Ident is not an enum variant; it is an enum variant constructor. The error message points out that it is a function:

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

Functions do not implement Debug. There is no solution for what you want.

like image 93
Shepmaster Avatar answered Sep 22 '25 07:09

Shepmaster