How to Parse TOML in Rust

Parse TOML in Rust by using the toml crate with serde to deserialize text into structs.

Use the toml crate to parse TOML files into Rust structs by defining a struct with #[derive(Deserialize)] and calling toml::from_str.

use serde::Deserialize;

#[derive(Deserialize)]
struct Config {
    name: String,
    version: u32,
}

fn main() {
    let toml_str = r#"
        name = "my-project"
        version = 1
    "#;
    let config: Config = toml::from_str(toml_str).unwrap();
    println!("{} v{}", config.name, config.version);
}