---
title: "Replace comments above a YAML key in Rust"
url: https://recursiveai.org/oneyaml/rust/cookbook/set-leading-yaml-comments/
description: "Replace the leading comments above one YAML key in Rust with set_leading, while keeping its value and same-line comment unchanged."
version: "1.2.0"
---

# Replace comments above a YAML key 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/set-leading-yaml-comments/set-leading-yaml-comments.zip)

Use `set_leading` to replace the comment above `port` without changing the value or the `# TLS` note beside it.

```shell
cargo add oneyaml
```

Requires Rust 1.85 or later.

```rust
use oneyaml::Document;

fn main() {
    let yaml = "# Old port\nport: 443 # TLS\n";
    let mut doc = Document::parse(yaml)
        .expect("valid YAML");
    doc.get_mut("port").expect("port")
        .set_leading(["HTTPS port"])
        .expect("replace comment");
    print!("{doc}");
}
```

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

`cargo run` prints:

```text
# HTTPS port
port: 443 # TLS
```



If the new comment would attach to a different entry, the edit returns `EditError::CommentReattaches` and leaves the YAML unchanged.

## Variations

To remove just the comments above the key, replace the `get_mut` chain with the one below. It prints `port: 443 # TLS`.

```rust
doc.get_mut("port").expect("port")
    .set_leading(oneyaml::NO_COMMENTS)
    .expect("remove leading comments");
```

## Related recipes

- [Remove all comments attached to a YAML key](https://recursiveai.org/oneyaml/rust/cookbook/clear-yaml-node-comments/)
- [Move a comment to another YAML key](https://recursiveai.org/oneyaml/rust/cookbook/move-yaml-comment-between-keys/)

## The short answer

Use `doc.get_mut("port")` to select the entry and pass the new comment text to `set_leading` without the `#` prefix. It replaces all of the entry’s leading comments, not just the first one. To edit `config.yaml`, save the inline sample in that file and run the program from the same directory. Replace the `let yaml` line with the read below, pass `&yaml` to `Document::parse`, and replace `print!("{doc}")` with the write. The edited YAML goes to `updated.yaml`; the original stays unchanged.

```rust
let yaml = std::fs::read_to_string("config.yaml").expect("read config.yaml");
// Parse &yaml and replace the comment as shown above.
std::fs::write("updated.yaml", doc.to_string()).expect("write updated.yaml");
```

## How do you write multiple comment lines above a YAML key?

Pass one string per line. Replace `["HTTPS port"]` with `["HTTPS port", "Use TLS"]`, and the output has two `#` lines immediately above `port`. When the key is nested, [`set_leading`](https://github.com/recursive-ai-oss/oneyaml-rust/blob/14445615f5a5b2895fdaa1da7719584ef15d9d6b/crates/oneyaml/src/document/comment_edit.rs#L772-L780) indents the new lines to match the entry and uses the document’s line ending. There’s no need to put indentation, `# `, or a line break into each string.

This is a replacement, so include every leading line you want after the edit, even one that was already there. Blank lines left by the old comments are kept. For a note on the same line as a value, use the entry’s [trailing-comment method](https://github.com/recursive-ai-oss/oneyaml-rust/blob/14445615f5a5b2895fdaa1da7719584ef15d9d6b/crates/oneyaml/src/document/edit.rs#L8474-L8477) instead.

## How do you update a YAML comment without changing the key or value?

Edit the parsed `Document`, rather than searching for the old text across the whole file. The same words may appear under another key or be part of a value. In the example, the entry selected by `get_mut("port")` is the only entry whose leading comments are replaced, so `443` and `# TLS` stay in place.

If a comment near the top of a file doesn’t get replaced, it may belong to the document header instead of the first key. For example, the [attachment rules](https://recursiveai.org/oneyaml/rust/docs/comment-attachment/#rule-2--document-header-blank-line-form) distinguish a header separated by a blank line from a leading comment immediately above an entry.
