Rust:
Working with TOML

How to:

// 1. Include 'toml' crate in your Cargo.toml
// [dependencies]
// toml = "0.5"

// 2. Deserialize TOML into a struct in Rust
use toml::Value;

fn main() {
    let toml_content = r#"
        [server]
        host = "localhost"
        port = 8080
    "#;

    let value = toml_content.parse::<Value>().unwrap();
    let host = value.get("server").unwrap().get("host").unwrap();
    let port = value.get("server").unwrap().get("port").unwrap();
    
    println!("The server is running on {}:{}", host, port);
    // Output: The server is running on "localhost":8080
}

Deep Dive

TOML, which stands for Tom’s Obvious, Minimal Language, was created by Tom Preston-Werner in 2013. It aims to be more readable than JSON or YAML for config files. TOML’s design focuses on unambiguous syntax, minimalism, and easy mapping to data types.

Alternatives to TOML include JSON, YAML, and XML, but TOML wins in scenarios where human readability and file editing by non-programmers is crucial. When working with TOML in Rust, serde provides a strong foundation for serialization and deserialization, using traits to map TOML onto Rust’s structs effortlessly.

A challenge while working with TOML is its strictness on types and structure. The programmer must define a well-structured Rust type system reflecting the schema of the TOML data to effectively utilize TOML in Rust.

See Also