Cargo.toml:
x
[dependencies]serde = { version = "1.0.130", features = ["derive"] }serde_json = "1.0"serde_yaml = "0.9.13"src/main.rs:
xxxxxxxxxxuse serde::{Deserialize, Serialize};
struct Config { one: i32, two: Vec<String>, three: TestEnum,}
enum TestEnum { One(i32), Two(String),}
fn main() { let yaml_string = r#"---one: 1two: - this is the first element - this is the second elementthree: !Two this is two in TestEnum "#; let config = serde_yaml::from_str::<Config>(&yaml_string).unwrap(); println!("{:#?}", config); println!("{:?}", config.three)}
输出:
x
Config { one: 1, two: [ "this is the first element", "this is the second element", ], three: Two( "this is two in TestEnum", ),}Two("this is two in TestEnum")