toml::de

Function from_str

Source
pub fn from_str<T>(s: &str) -> Result<T, Error>
Available on crate feature parse only.
Expand description

Deserializes a string into a type.

This function will attempt to interpret s as a TOML document and deserialize T from the document.

To deserializes TOML values, instead of documents, see ValueDeserializer.

ยงExamples

use serde::Deserialize;

#[derive(Deserialize)]
struct Config {
    title: String,
    owner: Owner,
}

#[derive(Deserialize)]
struct Owner {
    name: String,
}

let config: Config = toml::from_str(r#"
    title = 'TOML Example'

    [owner]
    name = 'Lisa'
"#).unwrap();

assert_eq!(config.title, "TOML Example");
assert_eq!(config.owner.name, "Lisa");