| Crates.io | enum-display |
| lib.rs | enum-display |
| version | 0.2.1 |
| created_at | 2022-09-28 04:15:41.094575+00 |
| updated_at | 2025-10-01 06:07:53.963254+00 |
| description | A macro to derive Display for enums |
| homepage | https://github.com/SeedyROM/enum-display |
| repository | https://github.com/SeedyROM/enum-display |
| max_upload_size | |
| id | 675412 |
| size | 20,224 |
enum-displayenum-display is a crate for implementing std::fmt::Display on enum variants with macros.
std (default): Enables standard library support for convenience methods like to_string()no_std: Core functionality that works in no_std environments without allocationno_std environmentsTo use in no_std mode, disable default features:
[dependencies]
enum-display = { version = "0.2.1", default-features = false }
The crate works without allocation by writing directly to the formatter:
#![no_std]
extern crate alloc;
use alloc::string::ToString;
use enum_display::EnumDisplay;
#[derive(EnumDisplay)]
enum Status {
Ready,
#[display("Error: {code}")]
Error { code: u32 },
}
fn main() {
// Works in no_std!
assert_eq!(Status::Ready.to_string(), "Ready");
}
use enum_display::EnumDisplay;
#[derive(EnumDisplay)]
enum Color {
Red,
Green,
Blue,
}
assert_eq!(Color::Red.to_string(), "Red");
assert_eq!(Color::Green.to_string(), "Green");
assert_eq!(Color::Blue.to_string(), "Blue");
Any case from convert_case is supported.
use enum_display::EnumDisplay;
#[derive(EnumDisplay)]
#[enum_display(case = "Kebab")]
enum Message {
HelloGreeting { name: String },
}
assert_eq!(Message::HelloGreeting { name: "Alice".to_string() }.to_string(), "hello-greeting");
#[display]The #[display] attribute allows you to customize how individual enum variants are formatted. This attribute accepts a format string that follows Rust's standard formatting syntax.
use enum_display::EnumDisplay;
#[derive(EnumDisplay)]
enum Status {
// Unit variant with custom text
#[display("System is ready")]
Ready,
// Using the variant name with {variant}
#[display("{variant}: Operation completed")]
Success,
}
assert_eq!(Status::Ready.to_string(), "System is ready");
assert_eq!(Status::Success.to_string(), "Success: Operation completed");
The #[display] attribute provides different ways to access variant data:
| Variant Type | Access Pattern | Example |
|---|---|---|
| Unit | {variant} only |
#[display("{variant} occurred")] |
| Named {...} | Field names | #[display("Error: {message} (code: {code})")] |
| Tuple (...) | Positional indices | #[display("Processing {0} of {1}")] |
use enum_display::EnumDisplay;
#[derive(EnumDisplay)]
enum Response {
#[display("Success: {message}")]
Success { message: String },
#[display("Error {code}: {description}")]
Error { code: u32, description: String },
}
let success = Response::Success {
message: "Data saved".to_string()
};
assert_eq!(success.to_string(), "Success: Data saved");
use enum_display::EnumDisplay;
#[derive(EnumDisplay)]
enum Progress {
#[display("Loading... {0}%")]
Loading(u8),
#[display("Processing item {0} of {1}")]
Processing(usize, usize),
}
assert_eq!(Progress::Loading(75).to_string(), "Loading... 75%");
assert_eq!(Progress::Processing(3, 10).to_string(), "Processing item 3 of 10");
The #[display] attribute supports all of Rust's format string features:
use enum_display::EnumDisplay;
#[derive(EnumDisplay)]
enum Metrics {
#[display("CPU: {usage:.1}%")]
CpuUsage { usage: f64 },
#[display("Memory: {used:>8} / {total:<8} bytes")]
Memory { used: usize, total: usize },
#[display("Temperature: {0:3}°C")]
Temperature(i32),
}