Bijective String - Debug?

Is Debug for String injective, and does the inverse already exist in std?

Sorry, I don’t understand the question. What problem are you trying to solve?

I will guess that you are asking whether

fn escape (s: String) -> String {
    format!("{:?}", s)
}

is injective or not. It is.

The inverse would require “unescaping” the escaped chars, which, at first glance, the ::unescape crate seems to do.


However, since this seems to be related to deserialization, mentioning serde is mandatory, as it provides the best tools for it.

3 Likes

Thank you!

However, be warned that the format of Debug is not guaranteed and may change at any time. Parsing of {:?} output is risky. It’s best to use your own escape function that guarantees to have reliable output.

1 Like

Thanks. I currently use the json crate for string (un)escape, and I guess I keep it that way.

1 Like