frozenlib / parse-display

Procedural macro to implement Display and FromStr using common settings.
Apache License 2.0
182 stars 14 forks source link

How to parse `{` and `}` characters? #37

Closed fritzrehde closed 10 months ago

fritzrehde commented 10 months ago

Suppose I would like to use:

#[derive(FromStr)]
#[display("{{a},{b}}")]
struct Ratingss {
    a: usize,
    b: usize,
}

to parse from the string "{42,100}". This leads to an invalid display format. Is this a bug, or not allowed/not implemented?

frozenlib commented 10 months ago

In parse-display, just like in std::fmt::format, { and } must be expressed as {{ and }}. So, it should be written as follows.

#[derive(FromStr, Debug)]
#[display("{{{a},{b}}}")]
struct Ratingss {
    a: usize,
    b: usize,
}
fritzrehde commented 10 months ago

Ah thanks, I didn't know that about the syntax to print { und } in std::fmt::format.