---
title: "Read YAML documents one at a time in Rust"
url: https://recursiveai.org/oneyaml/rust/cookbook/read-yaml-stream-one-document-at-a-time/
description: "Iterate a Rust YAML deserializer and process each document before reading the next, without collecting the decoded documents in a Vec."
version: "1.2.0"
---

# Read YAML documents one at a time in Rust

**oneyaml** is a free, open-source YAML library for Rust from Recursive AI. MIT or Apache-2.0, no unsafe code.

[crates.io](https://crates.io/crates/oneyaml) · [GitHub](https://github.com/recursive-ai-oss/oneyaml-rust) · [Docs](https://recursiveai.org/oneyaml/rust/docs/) · [Download example (.zip)](https://recursiveai.org/oneyaml/rust/cookbook/read-yaml-stream-one-document-at-a-time/read-yaml-stream-one-document-at-a-time.zip)

Loop over `Deserializer::from_str` and deserialize each YAML document before advancing.

```shell
cargo add oneyaml
cargo add serde
```

```rust
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](https://recursiveai.org/oneyaml/rust/cookbook/read-yaml-stream-one-document-at-a-time/read-yaml-stream-one-document-at-a-time.zip) and `cargo run`.

Requires Rust 1.85 or later. The example prints:

```text
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`.

```rust
let yaml = "web\n---\nworker";
for doc in Deserializer::from_str(yaml) {
    let name = String::deserialize(doc)?;
    println!("{name}");
}
```

## Related recipes

- [Read and edit a multi-document YAML file](https://recursiveai.org/oneyaml/rust/cookbook/kubernetes-multi-document/)
- [Read YAML without a schema](https://recursiveai.org/oneyaml/rust/cookbook/read-yaml-without-a-schema/)

## 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`](https://doc.rust-lang.org/std/fs/fn.read_to_string.html) 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`](https://docs.rs/oneyaml/1.2.0/oneyaml/fn.from_reader.html) 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](https://github.com/recursive-ai-oss/oneyaml-rust/blob/14445615f5a5b2895fdaa1da7719584ef15d9d6b/crates/oneyaml/src/de/api.rs#L405-L415) 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.
