---
title: "Read a YAML key containing a dot in Rust"
url: https://recursiveai.org/oneyaml/rust/cookbook/address-yaml-keys-containing-dots/
description: "Look up a literal dotted YAML key in Rust by quoting one document path segment. See how it differs from a nested path and how to use path! for dynamic keys."
version: "1.2.0"
---

# Read a YAML key containing a dot 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/address-yaml-keys-containing-dots/address-yaml-keys-containing-dots.zip)

Quote `app.name` in the lookup path.

```shell
cargo add oneyaml
```

```rust
use oneyaml::Document;
fn main() -> Result<(), oneyaml::Error> {
    let yaml = "labels: {app.name: web}\n";
    let doc = Document::parse(yaml)?;
    let path = r#"labels."app.name""#;
    let node = doc.get_path(path);
    let name = node.and_then(|n| n.as_str());
    println!("{name:?}");
    Ok(())
}
```

[The example](https://recursiveai.org/oneyaml/rust/cookbook/address-yaml-keys-containing-dots/address-yaml-keys-containing-dots.zip) is the full program below: unzip it and `cargo run`. To run only the code above, paste it into src/main.rs of a new `cargo new example` project and `cargo run`.

The literal dotted key gives:

```text
Some("web")
```

## Full program

The download uses Rust 1.85 or later and reads this `config.yaml`:

```yaml
labels: {app.name: web}
```

```rust
use oneyaml::Document;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let yaml = std::fs::read_to_string("config.yaml")?;
    let doc = Document::parse(&yaml)?;
    let node = doc.get_path(r#"labels."app.name""#);
    let name = node.and_then(|node| node.as_str());
    println!("{name:?}");
    Ok(())
}
```

`cargo run` prints:

```text
Some("web")
```

The lookup returns `None` if the path is malformed or names no node; `as_str` also returns `None` for a node that isn’t a string.

## Variations

**Use a key from a variable.** Replace the `let node` line. `path!` takes the key as one segment; it prints the same `Some("web")` without manual escaping.

```rust
let key = "app.name";
let node = doc.get_path(&oneyaml::path!["labels", key]);
```

## Related recipes

- [Look up a nested YAML value by JSON Pointer in Rust](https://recursiveai.org/oneyaml/rust/cookbook/select-yaml-node-by-json-pointer/)
- [Create a missing YAML key in Rust](https://recursiveai.org/oneyaml/rust/cookbook/create-missing-yaml-key/)

---

## The short answer

Use `get_path` with double quotes around the segment that contains a literal dot. With oneyaml-rust, the path `labels."app.name"` takes two steps: the `labels` mapping, then its `app.name` key. Rust’s raw string syntax (`r#"…"#`) keeps the inner quotes in the path without backslashes in your Rust code.

## How do you look up a YAML key that has a dot in its name?

The quotes belong in the **lookup path**, not necessarily in the YAML. That distinction matters when both forms exist under the same parent:

```yaml
labels:
  app.name: web
  app:
    name: worker
```

The path with a quoted segment reads `web`; the unquoted `labels.app.name` reads `worker`. The file has both keys, and neither lookup falls back to the other. If you quote `app.name` in the YAML but leave its lookup path unquoted, the path still takes three steps. See the [path syntax at version 1.2.0](https://docs.rs/oneyaml/1.2.0/oneyaml/trait.IntoPath.html).

For a key your program gets at runtime, supply the parts with `path!` as in the variation. Don’t insert the key into a dot-separated string: a dot in the key would make another step, and a quote could make the path malformed. In a string path, the same quoting rule applies to a key with a space or square bracket. To select a list item **after** a dotted key, put `[0]` outside the segment’s closing quote; `[0]` in the quotes would be part of the key. Path strings match string keys. If a mapping uses an integer as a key, use `NodeRef::get` on the parent with the integer key instead.
