How to escape curly braces in a format string in Rust

后端 未结 1 530
忘了有多久
忘了有多久 2020-12-11 00:20

I want to write this

write!(f, \"{ hash:{}, subject: {} }\", self.hash, self.subject)

But since curly braces have special meaning for forma

相关标签:
1条回答
  • 2020-12-11 01:03

    You might be reading some out of date docs (e.g. for Rust 0.9)

    As of Rust 1.0, the way to escape { and } is with another { or }

    write!(f, "{{ hash:{}, subject: {} }}", self.hash, self.subject)
    

    The literal characters { and } may be included in a string by preceding them with the same character. For example, the { character is escaped with {{ and the } character is escaped with }}.

    0 讨论(0)
提交回复
热议问题