Escaping and unescaping Unicode in ASCII

I would like to encode Unicode in ASCII using the escape sequences for string literals.

Does the standard library provide some functionality to implement this kind of escaping/unescaping?

    fn escape(s: &str) -> String {
        unimplemented!()
    }
    fn unescape(s: &str) -> String {
        unimplemented!()
    }
    fn main() {
        let str = "mixed ASCII and 🦀".to_string();
        let escaped_str = escape(&str);
        assert!(escaped_str.is_ascii());
        assert_eq!(str, unescape(&escaped_str));
    }

(Playground)

You probably want str::escape_default(&self). For details on the exact format, follow the link from that documentation to char::escape_default

Edit: playground link

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.