Module toml

Source
Expand description

TOML parsing and serialization.


toml provides parsing and serialization support for the TOML format. TOML is a configuration file format that’s designed to be easy to read and write, making it popular for configuration files in the Rust ecosystem, including Cargo.toml.

§Examples

Parsing TOML from a string:

let toml_str = "name = \"example\"\nversion = 1";
let parsed: toml::Value = toml::from_str(toml_str).unwrap();
assert_eq!(parsed["name"].as_str(), Some("example"));
assert_eq!(parsed["version"].as_integer(), Some(1));

Deserializing to a struct with serde:

use serde::Deserialize;

#[derive(Deserialize)]
struct Config {
    title: String,
    database: Database,
}

#[derive(Deserialize)]
struct Database {
    host: String,
    port: u16,
}

let toml_str = r#"title = "Example"

[database]
host = "localhost"
port = 5432
"#;

let config: Config = toml::from_str(toml_str).unwrap();
assert_eq!(config.title, "Example");
assert_eq!(config.database.host, "localhost");

Serializing a struct to TOML:

use serde::Serialize;

#[derive(Serialize)]
struct Config {
    title: String,
    debug: bool,
}

let config = Config {
    title: "My App".to_string(),
    debug: true,
};

let toml_string = toml::to_string(&config).unwrap();
assert!(toml_string.contains("title = \"My App\""));
assert!(toml_string.contains("debug = true"));

Modules§

de
Deserializing TOML into Rust structures.
map
A map of String to Value.
ser
Serializing Rust structures into TOML.
value
Definition of a TOML value

Macros§

toml
Construct a Table from TOML syntax.

Structs§

Deserializer
Deserialization for TOML documents.
Serializer
Serialization for TOML documents.
Spanned
A spanned value, indicating the range at which it is defined in the source.

Enums§

Value
Representation of a TOML value.

Functions§

from_slice
Deserializes bytes into a type.
from_str
Deserializes a string into a type.
to_string
Serialize the given data structure as a String of TOML.
to_string_pretty
Serialize the given data structure as a “pretty” String of TOML.

Type Aliases§

Table
Type representing a TOML table, payload of the Value::Table variant.