Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to print enum values?

Tags:

enums

rust

Is there an easy way to format and print enum values? I expected that they'd have a default implementation of std::fmt::Display, but that doesn't appear to be the case.

enum Suit {     Heart,     Diamond,     Spade,     Club }  fn main() {     let s: Suit = Suit::Heart;     println!("{}", s); } 

Desired output: Heart

Error:

error[E0277]: the trait bound `Suit: std::fmt::Display` is not satisfied   --> src/main.rs:10:20    | 10 |     println!("{}", s);    |                    ^ the trait `std::fmt::Display` is not implemented for `Suit`    |    = note: `Suit` cannot be formatted with the default formatter; try using `:?` instead if you are using a format string    = note: required by `std::fmt::Display::fmt` 
like image 326
Daniel Lucraft Avatar asked Jan 19 '15 12:01

Daniel Lucraft


People also ask

How do I get a list of all enum values?

The idea is to use the Enum. GetValues() method to get an array of the enum constants' values. To get an IEnumerable<T> of all the values in the enum, call Cast<T>() on the array. To get a list, call ToList() after casting.

Can we print enum in Java?

By default, when you print an enum constant, it prints its literal value e.g. if the name of the enum instance is RED, then it will print RED. This is also the value that is returned by the name() method of java. lang. Enum class.

How do I print an enum array?

You don't have to override toString method in each enum element. Every enum have method name() that returns String representation of element that invoked this method: SomeEnum.VALUE.name() will return "VALUE" String. You can just return name(). charAt(0)+name().

Can we get an enum by value?

Get the value of an EnumTo get the value of enum we can simply typecast it to its type. In the first example, the default type is int so we have to typecast it to int. Also, we can get the string value of that enum by using the ToString() method as below.


2 Answers

You can derive an implementation of std::format::Debug:

#[derive(Debug)] enum Suit {     Heart,     Diamond,     Spade,     Club }  fn main() {     let s = Suit::Heart;     println!("{:?}", s); } 

It is not possible to derive Display because Display is aimed at displaying to humans and the compiler cannot automatically decide what is an appropriate style for that case. Debug is intended for programmers, so an internals-exposing view can be automatically generated.

like image 188
DK. Avatar answered Sep 18 '22 19:09

DK.


The Debug trait prints out the name of the Enumvariant.

If you need to format the output, you can implement Display for your Enum like so:

use std::fmt;  enum Suit {     Heart,     Diamond,     Spade,     Club }  impl fmt::Display for Suit {     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {        match *self {            Suit::Heart => write!(f, "♥"),            Suit::Diamond => write!(f, "♦"),            Suit::Spade => write!(f, "♠"),            Suit::Club => write!(f, "♣"),        }     } }  fn main() {     let heart = Suit::Heart;     println!("{}", heart); } 
like image 32
Matilda Smeds Avatar answered Sep 19 '22 19:09

Matilda Smeds