Skip to content

format_strings breaks escaped character when string too long #6888

@t-webber

Description

@t-webber

Summary

Formatting this code:

fn main() {
    panic!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\nbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
}

Produces this code:

fn main() {
    panic!(
        "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\
         nbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
    )
}

Which adds some spaces after the newline and an extra n character.

Configuration

My rustfmt.toml:

format_strings = true

(unstable, tracking issue: #3353)

Reproduction Steps

  1. Copy the code from above in your main.rs and rustfmt.toml
  2. Run cargo fmt

Meta

$ rustfmt --version
rustfmt 1.9.0-nightly (fb0a5a5a9c 2026-05-08)
$ rustc --version
rustc 1.97.0-nightly (fb0a5a5a9 2026-05-08)

@rustbot label +S-has-mcve +UO-format_strings

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: this is a bug; use also I-* labels for specific bug kinds, e.g. I-non-idempotency or I-ICES-has-mcveStatus: a Minimal Complete and Verifiable Example has been found for this issueUO-format_stringsUnstable option: format_strings

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions