Why error: out of range hex escape

Hello guys,

TLDR: Why I cannot use "\xcc\x81" is my code

I would like to know why the compiler is so limitant string containing hex escape.

In this code

fn main() {
    let vec_grapheme = vec![0xcc, 0x81];
    let vec_to_string = String::from_utf8(vec_grapheme.to_owned()).unwrap_or("".to_string());
    println!("{}", vec_to_string);
    match vec_to_string.as_str() {
        "\xcc\x81" => {
            println!("char is {}", vec_to_string);
        _ => {
            println!("{}", vec_to_string);

I can't do the matching. Why ?

If you remove the match, the code works fine !


Note that If I do the matching with the weird "́" it works fine

You can only use \x for 7-bit codepoints: Tokens - The Rust Reference

For other characters, use \u.


The character in the string is \u{301} in case you are wondering.


Oh thanks.

I forgot about the \u !

Thanks a lot

1 Like

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.