# Escaping braces in Rust format strings

> Print literal curly braces in Rust format strings and combine escaped braces with captured values safely.

HTML: https://rustprint.com/format/escaping-braces/

Curly braces are syntax inside Rust format strings, so literal braces must be escaped. Double the character: `{{` emits `{`, and `}}` emits `}`.

**Literal braces around a captured value**

```rust
let name = "rust";
assert_eq!(format!("{{{name}}}"), "{rust}");
```

## Literal braces

The doubled pairs are escapes in the format string itself. They do not add another formatting argument, and Rust parses them as literal text around any real replacement fields.

That makes a pattern such as `{{{name}}}` valid. Read it as three parts: `{{` for a literal opening brace, `{name}` for the captured value, and `}}` for a literal closing brace.

## Why a single brace fails

A lone opening or closing brace is interpreted as formatting syntax. If it cannot form a valid replacement field, Rust rejects the format string at compile time.

This is useful failure behavior. You do not need a runtime escape pass, and malformed formatting syntax does not wait until production input reaches the code.

## JSON-looking and template-looking output

Escaped braces are useful when you need output that visually contains object or template delimiters. They only solve literal brace syntax, though. They do not serialize data or make a string valid JSON.

For actual data serialization, use a serializer rather than building a structured format manually with `format!`.

## Sources

- [std::fmt escaping](https://doc.rust-lang.org/std/fmt/#escaping)

## Related RustPrint guides

- [Rust format strings - syntax, width, precision, and traits](/format/format-strings/index.md): Read Rust format strings from {} through argument capture, width, precision, alignment, escaping, and formatting traits.
- [Rust format arguments: positional, named, and captured variables](/format/arguments-and-capture/index.md): Use implicit, positional, named, and captured arguments in Rust format strings, including println! with captured variables and how argument selection ends at the colon.
- [Rust format specifier grammar](/reference/format-specifier-grammar/index.md): Read the exact order of fill, alignment, sign, alternate, zero, width, precision, and type in a Rust format specification.
