Function serde_yml::de::from_str

source ·
pub fn from_str<'de, T>(s: &'de str) -> Result<T, Error>
where T: Deserialize<'de>,
Expand description

Deserialize an instance of type T from a string of YAML text.

This function takes a string slice containing YAML data and attempts to parse and deserialize it into an instance of the type T. The type must implement the Deserialize trait from Serde.

§Errors

This conversion can fail if the structure of the YAML does not match the structure expected by T, for example if T is a struct type but the YAML contains something other than a mapping. It can also fail if the structure is correct but T’s implementation of Deserialize decides that something is wrong with the data, for example required struct fields are missing from the YAML mapping or some number is too big to fit in the expected primitive type.

§Examples

use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct Person {
    name: String,
    age: u32,
}

let yaml_str = r#"
name: John Doe
age: 30
"#;

let person: Person = serde_yml::from_str(yaml_str).unwrap();
println!("{:?}", person);