Here's some code (blatantly copied from an SO answer):
fn main() {
let x = String::from_utf8(
"The compiler said “you have an error!”."
.bytes()
.flat_map(|b| std::ascii::escape_default(b))
.collect::<Vec<u8>>(),
)
.unwrap();
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.