How to escape curly braces in a format string in Rust?
Hello everyone! I just started learning Rust and ran into a problem when working with the `format!` macro (and `println!` too). I need to output a string that contains literal curly braces `{` and `}`. For example, I want to generate JSON-like output or simply display a mathematical expression like `{x: 5}`.
I tried writing this:
```rust
let x = 5;
println!("{x: {}}", x);
```
But the compiler gives an error: "invalid format string: expected `'}'` but string was terminated". I understand that curly braces in Rust are used as placeholders for value substitution. But how do I output the braces themselves as regular characters?
I tried escaping with a backslash `\{` — it doesn't work. I tried doubling them `{{` — the compiler started complaining differently. In other languages (Python, C#) I w
No answers yet.
Log in to leave an answer
Blogs