Strum
枚举转字符串库:Strum docs.rs/stum
使用示例:
[dependencies]
strum = { version = "0.21.0", features = ["derive"] }
#[macro_use]
extern crate strum;
use std::str::FromStr;
#[derive(EnumString, Display, Debug, PartialEq)]
pub enum Colors {
Red,
Green
}
fn main() {
let color: String = Colors::Red.to_string();
println!("{}", color); // Red
let new_color = "Green";
if Colors::Green == Colors::from_str(new_color).unwrap() {
println!("New color is {}", new_color); // New Color is Green
}
}
1
共 0 条评论, 1 页
评论区
写评论还没有评论