Loop over Deserializer::from_str and deserialize each YAML document before advancing.
cargo add oneyaml cargo add serde
use oneyaml::{Deserializer, Value}; use serde::Deserialize; fn main() -> Result<(), oneyaml::Error> { let yaml = "name: web\n---\nname: worker"; for doc in Deserializer::from_str(yaml) { let value = Value::deserialize(doc)?; let name = value["name"].as_str(); println!("{}", name.unwrap_or("(unnamed)")); } Ok(()) }
Paste the code above into src/main.rs of a new cargo new example project and cargo run, or unzip the example and cargo run.
Requires Rust 1.85 or later. The example prints:
web worker
A syntax or deserialization error stops the loop through ?. Documents already processed aren’t rolled back.
Variations
Read scalar documents as strings
Replace the input and loop in main with this code if each document is a string rather than a mapping. Remove Value from the import. The output is still web then worker.
let yaml = "web\n---\nworker"; for doc in Deserializer::from_str(yaml) { let name = String::deserialize(doc)?; println!("{name}"); }
Related recipes
The short answer
Call oneyaml::Deserializer::from_str on the YAML text. Iterate it and call Deserialize::deserialize for each item, inside the loop. You can process the decoded document and drop it before advancing. That avoids collecting a Vec of decoded documents. The example still holds the input string; it doesn’t read through an open file or socket in chunks.
How do you read multiple YAML documents from one file in Rust?
For a UTF-8 file, load the text with std::fs::read_to_string and give the deserializer a reference to the string. Keep the string alive while the iterator uses it. There’s no document iterator over a File or other Read in v1.2.0: oneyaml::from_reader buffers the input up to its size cap and expects only one document. The free oneyaml::from_str function also expects one document: Y3009 means it found more than one. The deserializer iterator is a different entry point, even though its constructor is also called from_str.
Let the parser find each document instead of splitting the text yourself. An indented --- in a block scalar can be part of the value, not a document start marker. If all documents have the same shape, deserialize them into the same type, as the string variation does. If they vary in shape, Value lets you look at keys like name without requiring a fixed struct.
Can I save the deserializers and read them later?
No. Each yielded deserializer refers to the current position in the same stream. If you advance the iterator before deserializing the current document, the parser skips it. If you need to save something for later, save the decoded document or the fields you need, not the deserializer itself.
Code that processes documents as it reads them can produce partial results. If the third document is malformed, the first two may already have been printed or written somewhere. When you need all-or-nothing behavior, stage the results before committing them. An empty stream or one containing only comments yields zero documents, so decide separately whether zero documents is valid for your application.