How to convert excaped bytes back to UTF-8

Here's some code (blatantly copied from an SO answer):

fn main() {
    let x = String::from_utf8(
        "The compiler said “you have an error!”."
            .flat_map(|b| std::ascii::escape_default(b))
    println!("{}", x);
    println!("{}", String::from_utf8(x.as_bytes().to_vec()).unwrap());

How can I recover the original UTF-8 encoded string from the ASCII escaped string? My attempt (in the second println!() statement) is clearly wrong.

I guess I should have included the playground link: Rust Playground

unescape source

I found a Rust implementation to unescape, but I do not know how good it is.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.