Recursive AI

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.

Use set_trailing to add # HTTPS after port: 443.

Terminal
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 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

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

All oneyaml Rust recipes