Rust:
Working with YAML

How to:

Rust doesn’t support YAML in its standard library, so we commonly use third-party crates like serde (for serializing and deserializing data) in combination with serde_yaml.

First, add dependencies to your Cargo.toml:

[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.8"

Now, let’s see how to deserialize a YAML string into a Rust struct and serialize a Rust struct back into a YAML string.

Deserializing YAML into Rust Structures

Define a Rust struct that mirrors the data you expect in YAML. Use Serde attributes for customization if needed.

use serde::{Deserialize, Serialize};
use serde_yaml;

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Config {
    name: String,
    durability: i32,
    owner: Owner,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Owner {
    name: String,
    age: i32,
}

fn main() {
    let yaml_data = "
name: Shield
durability: 300
owner:
  name: Steve
  age: 25
";

    let deserialized_config: Config = serde_yaml::from_str(yaml_data).unwrap();
    println!("{:?}", deserialized_config);
}

Sample output upon running the above Rust code would be:

Config { name: "Shield", durability: 300, owner: Owner { name: "Steve", age: 25 } }

Serializing Rust Structures into YAML

This example takes the Config struct from the previous section and serializes it back into YAML format.

fn main() {
    let config = Config {
        name: String::from("Axe"),
        durability: 120,
        owner: Owner {
            name: String::from("Alex"),
            age: 30,
        },
    };

    let serialized_yaml = serde_yaml::to_string(&config).unwrap();
    println!("{}", serialized_yaml);
}

Expected output will be a YAML-formatted string:

---
name: Axe
durability: 120
owner:
  name: Alex
  age: 30

These snippets demonstrate how to integrate YAML parsing and generation in your Rust applications efficiently, using the popular serde and serde_yaml crates, accommodating complex data structures and providing simple, human-readable configurations.