[][src]Derive Macro strum_macros::IntoStaticStr

#[derive(IntoStaticStr)]
{
    // Attributes available to this derive:
    #[strum]
}

Implements From<MyEnum> for &'static str on an enum.

Implements From<YourEnum> and From<&'a YourEnum> for &'static str. This is useful for turning an enum variant into a static string. The Rust std provides a blanket impl of the reverse direction - i.e. impl Into<&'static str> for YourEnum.

use strum_macros::IntoStaticStr;

#[derive(IntoStaticStr)]
enum State<'a> {
    Initial(&'a str),
    Finished,
}

fn verify_state<'a>(s: &'a str) {
    let mut state = State::Initial(s);
    // The following won't work because the lifetime is incorrect:
    // let wrong: &'static str = state.as_ref();
    // using the trait implemented by the derive works however:
    let right: &'static str = state.into();
    assert_eq!("Initial", right);
    state = State::Finished;
    let done: &'static str = state.into();
    assert_eq!("Finished", done);
}

verify_state(&"hello world".to_string());