---
title: "Add an inline YAML comment in Rust"
url: https://recursiveai.org/oneyaml/rust/cookbook/add-inline-yaml-comment/
description: "Use set_trailing to add a same-line YAML comment after a value in Rust. Keep the value and any comments above its key."
version: "1.2.0"
---

# Add an inline YAML comment 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/add-inline-yaml-comment/add-inline-yaml-comment.zip)

Use `set_trailing` to add `# HTTPS` after `port: 443`.

```shell
cargo add oneyaml
```

```rust
use oneyaml::Document;
fn main() {
    let yaml = "# Public listener\nport: 443\n";
    let mut doc = Document::parse(yaml)
        .expect("valid YAML");
    let mut port = doc.get_mut("port").unwrap();
    port.set_trailing(Some("HTTPS")).unwrap();
    print!("{doc}");
}
```

[The example](https://recursiveai.org/oneyaml/rust/cookbook/add-inline-yaml-comment/add-inline-yaml-comment.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 value and leading comment stay in place:

```text
# Public listener
port: 443 # HTTPS
```

## Full program

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

```yaml
# Public listener
port: 443
```

```rust
use oneyaml::Document;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let yaml = std::fs::read_to_string("config.yaml")?;
    let mut doc = Document::parse(&yaml)?;
    doc.get_mut("port")?.set_trailing(Some("HTTPS"))?;
    print!("{doc}");
    Ok(())
}
```

`cargo run` prints:

```text
# Public listener
port: 443 # HTTPS
```

If `port` is missing, the lookup fails before the edit; if the edit itself fails, the document stays unchanged.

## Variations

**Remove an old inline comment.** In the snippet at the top, replace the input and the edit with these lines:

```rust
let yaml = "# Public listener\nport: 443 # old\n";
let mut doc = Document::parse(yaml).expect("valid YAML");
doc.get_mut("port").expect("port")
    .set_trailing(None).expect("remove comment");
```

The existing `print!` then prints:

```text
# Public listener
port: 443
```

## Related recipes

- [Replace comments above a YAML key](https://recursiveai.org/oneyaml/rust/cookbook/set-leading-yaml-comments/)
- [Find which YAML entry owns a comment](https://recursiveai.org/oneyaml/rust/cookbook/explain-yaml-comment-owner/)

---

## The short answer

Parse the input as an editable `Document`, get the entry you want to annotate, and call `set_trailing(Some("HTTPS"))`. Pass the comment’s text, not the `#` marker. In oneyaml-rust, this edits the entry as written while leaving the integer value `443` and the leading `# Public listener` note alone. The example prints the edited YAML rather than overwriting the input file.

## Can you add inline comments in YAML configuration files?

Yes. A same-line comment starts with `#` separated from the preceding value by whitespace. YAML treats the note as presentation, not as part of the value; see [YAML 1.2.2’s comments section](https://yaml.org/spec/1.2.2/#66-comments). Putting the marker inside a quoted value instead makes it part of the string. That’s why adding ` # HTTPS` to the value itself is not equivalent to editing the comment: it can turn a number into a string.

If your entry already has a trailing comment, `set_trailing(Some(...))` replaces its text, keeping the whitespace before the existing `#`. Use `None` to remove it without clearing the leading comment. This is useful when regenerating a note: you don’t have to detect whether you wrote one on a previous run.

## Does YAML support multiline comments?

YAML has no block comment delimiter. For a note spanning several lines, write a separate `#` on each line. In the document API, use `set_leading` with one string per line to place that note above the entry; it replaces the existing leading comments, so include any you want to keep. Use `set_trailing` for a single line. It rejects a line break in the comment text rather than turning subsequent text into more YAML. If several lines of explanation are necessary, put them above the key and reserve the trailing comment for a short note such as a unit or protocol.
