---
title: "Read a YAML tag in Rust"
url: https://recursiveai.org/oneyaml/rust/cookbook/read-yaml-node-tag/
description: "Read the explicit tag on a value in a parsed YAML document in Rust. Get the resolved name of a tag such as !!str and handle a value with no tag."
version: "1.2.0"
---

# Read a YAML tag 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-node-tag/read-yaml-node-tag.zip)

A YAML tag labels a value’s type. Use `tag()` to read the explicit tag on `port: !!str 80`, where `!!str` marks `80` as a string.

```shell
cargo add oneyaml
```

Requires Rust 1.85 or later.

```rust
use oneyaml::Document;

fn main() {
    let yaml = "port: !!str 80\n";
    let doc = Document::parse(yaml)
        .expect("valid YAML");
    let port = doc.get("port").expect("port");
    let tag = port.tag().expect("resolved tag");
    match tag {
        Some(tag) => println!("{tag}"),
        None => println!("No explicit tag"),
    }
}
```

Unzip [the example](https://recursiveai.org/oneyaml/rust/cookbook/read-yaml-node-tag/read-yaml-node-tag.zip), then `cargo run`.

`cargo run` prints:

```text
tag:yaml.org,2002:str
```



An untagged node gives `Ok(None)`; a missing key is a separate `None` from `get`. [`tag()` returns `Err` only if a tag fails to resolve](https://github.com/recursive-ai-oss/oneyaml-rust/blob/14445615f5a5b2895fdaa1da7719584ef15d9d6b/crates/oneyaml/src/document/view.rs#L480-L486), which `Document::parse` rules out before the `expect` on `tag()` runs.

## Variations

To see the no-tag case, change the first statement inside `main` as shown below. The program prints `No explicit tag`.

```rust
let yaml = "port: 80\n";
```

## Related recipes

- [Deserialize YAML tags into Rust enums](https://recursiveai.org/oneyaml/rust/cookbook/deserialize-yaml-tagged-enums/)
- [Read YAML without a struct](https://recursiveai.org/oneyaml/rust/cookbook/read-yaml-without-a-schema/)

## The short answer

Parse with `Document::parse`, look up the value with `get`, and call `tag()` on it. In this input the name is `tag:yaml.org,2002:str`, not the shorthand `!!str`. You don’t need a Rust struct to inspect it.

## How do I read a YAML file in Rust and get a tag from it?

Save `port: !!str 80` in `config.yaml`. Run from the directory that contains the file. Replace the `let yaml` and `let doc` statements with these lines; leave the lookup and the tag check unchanged:

```rust
let yaml = std::fs::read_to_string("config.yaml")
    .expect("read config.yaml");
let doc = Document::parse(&yaml).expect("valid YAML");
```

If `port` is absent, there’s no node to inspect; if it exists but has no explicit tag, you get a node and `tag()` returns `Ok(None)`.

## What does a YAML tag like `!!str` mean?

The string tag tells a [YAML processor](https://yaml.org/spec/1.2.2/#10212-string) to treat the value as a string. [`tag()`](https://github.com/recursive-ai-oss/oneyaml-rust/blob/14445615f5a5b2895fdaa1da7719584ef15d9d6b/crates/oneyaml/src/document/view.rs#L472-L486) reads the explicit tag and expands its shorthand. That lets you compare the resolved name rather than checking whether the input happened to spell it `!!str`.

Reading a tag isn’t the same as asking what type the parser infers. `port: "80"` can be a string without an explicit `!!str`, so the tag lookup still returns `None`. Use this method when you care whether the author actually wrote a tag, not when you only need the value’s type.
