expression

Escape untrusted text before displaying it in a diagnostic

A filename or external label can contain control characters. For a plain-text diagnostic, escaping debug-sensitive characters prevents embedded newline and escape characters from being emitted literally. This is not a universal sanitizer for HTML, shell commands or logs; avoid printing secrets altogether. Fixture: Expected stdout (not executed): (empty) Expected stderr: 'invalid label: bad\\nname\\u{1b}[31m\n' Expected exit: 0 Common mistake: Printing untrusted control sequences directly to a terminal.

Formal Syntax & Structure

rust syntaxrust
input.escape_debug()

Working Example

Example Snippetrust
fn main() {    let label = "bad\nname\x1b[31m";    let safe = label.escape_debug().to_string();    assert!(!safe.contains('\n'));    assert!(!safe.contains('\x1b'));    eprintln!("invalid label: {safe}");}
Rust Reference