Function toml::ser::to_string

source · []
pub fn to_string<T: ?Sized>(value: &T) -> Result<String, Error> where
    T: Serialize
Expand description

Serialize the given data structure as a String of TOML.

Serialization can fail if T’s implementation of Serialize decides to fail, if T contains a map with non-string keys, or if T attempts to serialize an unsupported datatype such as an enum, tuple, or tuple struct.

Examples

use serde_derive::Serialize;

#[derive(Serialize)]
struct Config {
    database: Database,
}

#[derive(Serialize)]
struct Database {
    ip: String,
    port: Vec<u16>,
    connection_max: u32,
    enabled: bool,
}

fn main() {
    let config = Config {
        database: Database {
            ip: "192.168.1.1".to_string(),
            port: vec![8001, 8002, 8003],
            connection_max: 5000,
            enabled: false,
        },
    };

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