#enums #macro-derive #display

no-std enum-display

A macro to derive Display for enums

7 releases

0.2.1 Oct 1, 2025
0.2.0 Oct 1, 2025
0.1.4 Feb 9, 2024
0.1.3 Sep 28, 2022

#336 in Rust patterns

Download history 3541/week @ 2025-08-19 4146/week @ 2025-08-26 3653/week @ 2025-09-02 4801/week @ 2025-09-09 3553/week @ 2025-09-16 6116/week @ 2025-09-23 5314/week @ 2025-09-30 3140/week @ 2025-10-07 4184/week @ 2025-10-14 3684/week @ 2025-10-21 5004/week @ 2025-10-28 4754/week @ 2025-11-04 3606/week @ 2025-11-11 25566/week @ 2025-11-18 36995/week @ 2025-11-25 36872/week @ 2025-12-02

103,871 downloads per month
Used in 31 crates (12 directly)

MIT license

14KB
207 lines

enum-display

GitHub crates.io version docs.rs docs crates.io version CI build

enum-display is a crate for implementing std::fmt::Display on enum variants with macros.

Features

  • std (default): Enables standard library support for convenience methods like to_string()
  • no_std: Core functionality that works in no_std environments without allocation

Using in no_std environments

To 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");
}

Simple Example

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");

Example With Custom Case Transform

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");

Custom Variant Formatting with #[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.

Basic Usage

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");

Field Access Patterns

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}")]

Named Fields Example

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");

Tuple Fields Example

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");

Advanced Formatting

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),
}

Dependencies

~3.5–5MB
~96K SLoC