Skip to main content

from_str_multiple

Function from_str_multiple 

Source
pub fn from_str_multiple<'de, T, C>(input: &'de str) -> Result<C, Error>
where T: Deserialize<'de>, C: Default + Extend<T>,
Expand description

Deserialize multiple YAML documents from a string into a collection, supporting borrowed values. Empty, untagged null-like, and explicit !!null documents are skipped, as with from_multiple. Application-tagged roots are deserialized, even with null payloads.

The collection is initialized with Default and extended with each document in input order. Any C: Default + Extend<T> is supported, including Vec<T>, VecDeque<T>, and sets. The collection determines how ordering and duplicate values are handled. Specify both types when using turbofish syntax, for example ::<String, Vec<_>>. If the collection implements Extend for multiple item types, specify T explicitly to resolve ambiguity, for example ::<i32, Vec<_>>. Use from_multiple to return a Vec<T> with a single type argument.

This function supports both owned types (like String) and borrowed types (like &str). Borrowed values refer to the input string and cannot outlive it.

Strings can be borrowed when their parsed value exists verbatim in the input, including plain scalars and simple quoted strings. Strings requiring escape processing, folding, or other transformations cannot be deserialized into &str; use String or a #[serde(borrow)]-annotated Cow<'a, str> field to handle those values.

use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct Person<'a> {
    name: &'a str,
}

let yaml = String::from("name: Alice\n---\nname: Bob\n");
let people: Vec<Person<'_>> = serde_saphyr::from_str_multiple(&yaml)?;
assert_eq!(people[0].name, "Alice");
assert_eq!(people[1].name, "Bob");