-
Notifications
You must be signed in to change notification settings - Fork 151
Derive IntoStaticStr
Peter Glotfelty edited this page Aug 18, 2019
·
1 revision
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
.
extern crate strum;
#[macro_use] extern crate strum_macros;
#[derive(IntoStaticStr)]
enum State<'a> {
Initial(&'a str),
Finished
}
fn print_state<'a>(s:&'a str) {
let state = State::Initial(s);
// The following won't work because the lifetime is incorrect so we can use.as_static() instead.
// let wrong: &'static str = state.as_ref();
let right: &'static str = state.into();
println!("{}", right);
}
fn main() {
print_state(&"hello world".to_string())
}