[][src]Derive Macro strum::AsRefStr

    // Attributes available to this derive:

Converts enum variants to &'static str.

Implements AsRef<str> on your enum using the same rules as Display for determining what string is returned. The difference is that as_ref() returns a &str instead of a String so you don't allocate any additional memory with each call.

// You need to bring the AsRef trait into scope to use it
use std::convert::AsRef;
use strum_macros::AsRefStr;

#[derive(AsRefStr, Debug)]
enum Color {
    #[strum(serialize = "redred")]
    Green {
        range: usize,

// uses the serialize string for Display
let red = Color::Red;
assert_eq!("redred", red.as_ref());
// by default the variants Name
let yellow = Color::Yellow;
assert_eq!("Yellow", yellow.as_ref());
// or for string formatting
    "blue: {} green: {}",
    Color::Green { range: 42 }.as_ref()