Can't make sense of syntax error in match expression

trait DataLen {
    fn len(&self) -> usize;
}

impl DataLen for &str {
    fn len(&self) -> usize {
        let mut len = self.len();
        for pair in self.as_bytes().windows(2) {
            len -= 2;
            match pair {
                (r#"\\"#).as_bytes() | (r#"\""#).as_bytes() => len -= 2,
                (r#"\x"#).as_bytes() => len -= 4,
                _ => {}
            }
        }
        len
    }
}

Compiler says that it expected one of =>, if, or |. But as you can probably notice I am trying to match pair against (r#"\\"#).as_bytes()

You can't match against expressions like that. In general, you'd need to use consts or ==, for this particular case you should try using byte string literals like br#"\\"# in place of (r#"\\"#).as_bytes().

1 Like

Since the left hand side of the match arm takes pattern, not expression.

https://doc.rust-lang.org/stable/book/ch06-02-match.html

https://doc.rust-lang.org/stable/book/ch18-00-patterns.html

2 Likes