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,
                _ => {}

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.


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.